4 line Java Solution with O(n)


  • 0
    R
    public void moveZeroes(int[] nums) {
        for (int i = 0, j = 0; i < nums.length; i++, j++){
            while (j < nums.length && nums[j] == 0) j++;
            nums[i] = j >= nums.length ? 0 : nums[j];
        }
    }

Log in to reply
 

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