Java code using 2 pointers


  • 0
    Y
    public void moveZeroes(int[] nums) {
        int zero_num = 0;
        for(int i = 0; i < nums.length; i++){
            if(nums[i] == 0) {
                zero_num++;
            }
            else{
                nums[i - zero_num] = nums[i];
                if(zero_num != 0)
                    nums[i] = 0;
            }
        }
    }

Log in to reply
 

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