283. Move Zeroes

Solution

/**
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */
 var moveZeroes = function(nums) {
    let z = 0;
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] !== 0) {
            const temp = nums[z];
            nums[z] = nums[i];
            nums[i] = temp; // 0
            z++;
        }
    }
};

Complexity

  • Time Complexity: O(N), where N is the size of nums.
  • Space Complexity: O(1).

All Solutions