Java O(n) Solution


  • 6
    A
    public class Solution {
        public void moveZeroes(int[] nums) {
            int zeroNum = 0;
        
            for(int i=0; i < nums.length; i++)
                if(nums[i] == 0)
                    zeroNum++;
                else
                    nums[i-zeroNum] = nums[i];
                
            for(int i=0; i < zeroNum; i++)
                nums[nums.length - zeroNum + i] = 0;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.