JavaScript O(n) no swap no extra space on the array.


  • 0
    O
    var moveZeroes = function (nums) {
        var zeroes = 0;
        for (var i = 0; i < nums.length; i++) {
            if (nums[i] === 0) {
                nums.splice(i, 1);
                i--;
                zeroes++;
            }
        }
        while (zeroes) {
            nums.push(0);
            zeroes--;
        }
    };

Log in to reply
 

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