Skip to content

Commit 2e826d0

Browse files
authored
Merge pull request #1596 from aadil42/patch-37
2 parents 17b4cc6 + 5e3a2e4 commit 2e826d0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

javascript/283-Move-Zeroes.js

+26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
/**
2+
* Two Pointer
3+
* Time O(N) | Space O(N)
4+
* https://leetcode.com/problems/move-zeroes/
5+
* @param {number[]} nums
6+
* @return {void} Do not return anything, modify nums in-place instead.
7+
*/
8+
var moveZeroes = function(nums) {
9+
10+
const arr = new Array(nums.length).fill(0);
11+
12+
let [left, right] = [0, 0];
13+
14+
while (right < nums.length) {
15+
const isZero = (nums[right] === 0);
16+
if (!isZero) {
17+
arr[left] = nums[right];
18+
left++;
19+
}
20+
21+
right++;
22+
}
23+
24+
return arr;
25+
};
26+
127
/**
228
* 2 Pointer
329
* Time O(N) | Space O(1)

0 commit comments

Comments
 (0)