Skip to content

Commit d6c4d91

Browse files
author
baochau.dinh
committed
js-concepts: leetcode prob.283 - move zeroes
1 parent 59b9d59 commit d6c4d91

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

LeetCode/283. Move Zeroes/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var moveZeroes = function(nums) {
2+
if (nums.length === 1) return nums;
3+
4+
let count = 0;
5+
for (let i = 0; i < nums.length; i++) {
6+
if (nums[i] === 0) {
7+
count++;
8+
nums.splice(i, 1);
9+
i--;
10+
}
11+
}
12+
13+
for (let i = 1; i <= count; i++) {
14+
nums.push(0);
15+
}
16+
17+
return nums;
18+
};
19+
20+
console.log(moveZeroes([0, 0, 1]));

0 commit comments

Comments
 (0)