-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfind-all-numbers-disappeared-in-an-array.ts
65 lines (57 loc) · 1.34 KB
/
find-all-numbers-disappeared-in-an-array.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/
*/
/**
* 排序后查找
*/
// const findDisappearedNumbers = nums => {
// if (!nums.length) return nums;
// nums.sort((a, b) => a - b);
// let result = [];
// let [n, i] = [1, 0];
// while (n <= nums.length) {
// if (n < nums[i] || i >= nums.length) {
// result.push(n);
// n++;
// } else if (n > nums[i]) {
// i++;
// } else {
// n++;
// i++;
// }
// }
// return result;
// };
/**
* 桶排序
*/
// const findDisappearedNumbers = nums => {
// let i = 0
// while(i < nums.length) {
// if(nums[nums[i] - 1] !== nums[i]) {
// [nums[nums[i] - 1], nums[i]] = [nums[i], nums[nums[i] - 1]]
// } else {
// i++
// }
// }
// let result = []
// for(i = 0; i < nums.length; i++) {
// if(i + 1 !== nums[i]) result.push(i + 1)
// }
// return result
// }
/**
* 遍历 nums 将 nums[nums[i] - 1] 标记为负数。
* 最后没被标记的位置就是所求。
*/
const findDisappearedNumbers = nums => {
for (let i = 0; i < nums.length; i++) {
const index = Math.abs(nums[i]) - 1;
nums[index] = nums[index] > 0 ? -nums[index] : nums[index];
}
let result = [];
for (let i = 0; i < nums.length; i++) {
if (nums[i] > 0) result.push(i + 1);
}
return result;
};