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