Skip to main content

Leetcode - 283. Move Zeroes

· One min read

https://leetcode.com/problems/move-zeroes/description/

Solution 1

/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function (nums) {
let zeroCount = 0;

for (let i = 0; i < nums.length; i++) {
if (nums[i] !== 0) {
const zero = nums[zeroCount];
nums[zeroCount] = nums[i];
nums[i] = zero;
zeroCount++;
}
}

return nums;
};

Runtime 147 ms Beats 64.1%

Memory 47 MB Beats 27.90%