Given an array nums , write a function to move all's to the 0 end of it while maintaining the relative order of the No N-zero elements.
For example, given nums = [0, 1, 0, 3, 12] , after calling your function, nums should is [1, 3, 12, 0, 0] .
Note:
- You must does this in-place without making a copy of the array.
- Minimize The total number of operations.
Given an array, put all 0 behind the array, all non-0 elements are placed in front, and the order of non-0 elements is guaranteed to be unchanged.
Problem-solving ideas, from the past to traverse, meet 0 cnt++, meet the non-0 to move the CNT, and finally after the CNT set to 0 can be.
Public voidMovezeroes (int[] nums) { if(nums==NULL|| Nums.length==0){ return; } intCNT = 0; for(inti=0;i<nums.length;i++){ if(nums[i]==0) {CNT++; }Else{nums[i-cnt]=Nums[i]; } } if(cnt>0) {Arrays.fill (nums,nums.length-cnt,nums.length,0); } }
Move Zeroes--leetcode