Simple O(N) time O(1) space solution in Java


  • 0
    C
    public void moveZeroes(int[] nums) {
        if(nums==null||nums.length==0)  return;
        int zero = 0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]==0){
                zero++;
            }else{
                if(zero !=0){
                    nums[i-zero]=nums[i];
                    nums[i]=0;
                }
            }
        }
    }

Log in to reply
 

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