-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path283.移动零.js
52 lines (49 loc) · 994 Bytes
/
283.移动零.js
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
45
46
47
48
49
50
51
52
/*
* @lc app=leetcode.cn id=283 lang=javascript
*
* [283] 移动零
*/
// @lc code=start
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
// v1
// time complexity O(n)
// space complexity O(1)
// var moveZeroes1 = function(nums) {
// let k = 0
// for (let i = 0; i < nums.length; i++) {
// if (nums[i]) {
// nums[k] = nums[i]
// k++
// }
// }
// for (let j = k; j < nums.length; j++) {
// nums[j] = 0
// }
// return nums
// }
// v2
// time complexity O(n)
// space complexity O(1)
// preserve [0, k] as none-zero collections
var moveZeroes = function(nums) {
let slow = 0
for (let fast = 0; fast < nums.length; fast++) {
if (nums[fast] !== 0) {
if (fast !== slow) {
swap(slow, fast)
}
slow++
}
}
return nums
function swap(a, b) {
let temp = nums[b]
nums[b] = nums[a]
nums[a] = temp
}
}
// @lc code=end
module.exports = { moveZeroes }