Simple javascript solution


  • 8
    A

    The idea is remove each 0 and add 0 at the end of array, so I use reverse iterate to do it!

    var moveZeroes = function(nums) {       
        for(var i = nums.length;i--;){
            if(nums[i]===0){
                nums.splice(i,1)
                nums.push(0);
            }
        }
    };

  • 0
    H

    Although this is correct. This wouldn't be in place would it since we will be altering the array's size?


Log in to reply
 

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