A few lines of JavaScript, one pass


  • 0
    var moveZeroes = function(nums) {
        for (let zeroIndex = 0, nonzeroIndex = 0; nonzeroIndex < nums.length; nonzeroIndex++) {
            if (!nums[nonzeroIndex]) continue;
            [nums[zeroIndex], nums[nonzeroIndex]] = [nums[nonzeroIndex], nums[zeroIndex]];
            zeroIndex++;
        }
    };
    

Log in to reply
 

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