-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove-zeroes.js
More file actions
44 lines (37 loc) · 867 Bytes
/
move-zeroes.js
File metadata and controls
44 lines (37 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function (nums) {
// 双指针 (二刷的写法)
let slowIndex = 0;
for (let fastIndex = 1; fastIndex < nums.length; fastIndex++) {
if (nums[slowIndex] === 0) {
if (nums[fastIndex] !== 0) {
swap(nums, slowIndex++, fastIndex);
}
} else {
slowIndex++;
}
}
// let index = 0;
// for (let i = 0; i < nums.length; i++) {
// if (nums[i] !== 0) {
// nums[index] = nums[i];
// index++;
// }
// }
// for (let i = index; i < nums.length; i++) {
// nums[i] = 0;
// }
// 双指针
// let slow = 0;
// let fast = 0;
// while (fast < nums.length) {
// if (nums[fast] !== 0) {
// swap(nums, slow, fast);
// slow++;
// }
// fast++;
// }
};