Java Solution


  • 2
    T
    public void moveZeroes(int[] nums) {
        int zeros = 0;
        for(int i=0; i<nums.length; i++) {
            int val = nums[i];
            if(val == 0) {
                zeros++;
            } else {
                nums[i] = 0;
                nums[i - zeros] = val;
            }
        }
    }
    

  • 0
    L

    Great. Just to remember the number of zeros between right boundary of processed data and the left boundary of the to-do data.


Log in to reply
 

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