3 lines JavaScript solution


  • 0
    Z
    /**
     * @param {number[]} nums
     * @return {void} Do not return anything, modify nums in-place instead.
     */
    var moveZeroes = function(nums) {
        if (nums === null || nums.length === 0) return;
        for (var i = 0,insertPos = 0; i< nums.length;i++) if (nums[i] !== 0) {nums[insertPos] = nums[i];insertPos++;}//1st part of array, all positive numbers
        while (insertPos < nums.length) nums[insertPos++] = 0;// 2nd part of array, all 0
    };
    

Log in to reply
 

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