Skip to content

Commit 151a2a4

Browse files
committed
✅ [283]
1 parent 25fe84d commit 151a2a4

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

283/my_solution.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {void} Do not return anything, modify nums in-place instead.
4+
*/
5+
var moveZeroes = function (nums) {
6+
const len = nums.length
7+
let i = 0, visit = 0
8+
// console.log()
9+
console.log(nums)
10+
while (visit < len) {
11+
visit++;
12+
console.log(`~> i: ${i}, nums[i]: ${nums[i]}`)
13+
if (nums[i] === 0) {
14+
console.log();
15+
nums.splice(i, 1);
16+
console.log("b4");
17+
console.log(nums);
18+
nums[len - 1] = 0;
19+
console.log("afyer");
20+
console.log(nums);
21+
} else {
22+
i++;
23+
}
24+
}
25+
console.log(nums)
26+
};
27+
28+
// moveZeroes([0, 1, 0, 3, 12])
29+
moveZeroes([0, 0, 1])

283/solution.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {void} Do not return anything, modify nums in-place instead.
4+
*/
5+
var moveZeroes = function (nums) {
6+
const len = nums.length
7+
let i = 0, visit = 0
8+
9+
while (visit < len) {
10+
visit++;
11+
if (nums[i] === 0) {
12+
nums.splice(i, 1);
13+
nums[len - 1] = 0;
14+
} else {
15+
i++;
16+
}
17+
}
18+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
- [242. Valid Anagram](./242/)
7171
- [268. Missing Number](./268/)
7272
- [271. Encode and Decode Strings (Premium)](./271/)
73+
- [283. Move Zeroes](./283/)
7374
- [295. Find Median from Data Stream](./295/)
7475
- [297. Serialize and Deserialize Binary Tree](./297/)
7576
- [345. Reverse Vowels of a String](./345/)
@@ -159,7 +160,7 @@ Batch create:
159160
NOTE: JS IS HERE
160161
-->
161162
```ssh
162-
chapter=443 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
163+
chapter=283 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
163164
```
164165
> then you can use `x` for quick debug.
165166

0 commit comments

Comments
 (0)