Lintcode's Move Zero

Source: Internet
Author: User

Title Description:

Analysis: because to keep non-0 elements in the order of the original array, I can only think of finding a 0 o'clock, moving the array element one after another to overwrite the previous element, and then move the 0 to the back.

My Code:

1  Public classSolution {2     /*3 * @param nums:an integer array4 * @return:5      */6      Public voidMovezeroes (int[] nums) {7         //Write your code here8         //returns directly when the array is empty9         if(Nums.length = = 0) {Ten             return; One         } A         Booleanb =true; -         //determines whether the array is all 0, and if so, returns directly -          for(inti=0; i<nums.length; i++) { the             if(nums[i]! = 0) { -b =false; -             } -         } +         if(b = =true) { -             return ; +         } A          at         intK = Nums.length-1; -         inti =K; -          while(I >= 0) { -             //looking forward to the number of elements with a value of 0 -              while(I>=0 && nums[i]!=0) { -i--; in             } -             if(I >= 0) { to                 inttemp =Nums[i]; +                  for(intJ=i; j<k; J + +) { -NUMS[J] = nums[j+1]; the                 } *NUMS[K] =temp; $k--;Panax Notoginseng             } -         } the     } +}

Lintcode's Move Zero

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.