-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path448.找到所有数组中消失的数字.cpp
51 lines (43 loc) · 1.01 KB
/
448.找到所有数组中消失的数字.cpp
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
/*
* @Author: your name
* @Date: 2021-05-01 15:36:41
* @LastEditTime: 2021-05-01 17:17:08
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \project\Leetcode\448.找到所有数组中消失的数字.cpp
*/
/*
* @lc app=leetcode.cn id=448 lang=cpp
*
* [448] 找到所有数组中消失的数字
*/
// @lc code=start
/* 0 1 2 3 4 5 6 7
[value ] 4 3 2 7 8 2 3 1
[value - 1] 3 2 1 6 7 1 2 0
0 4 -4
1 3 -3
2 2 -2
3 7 -7
4 8 8
5 2 2
6 3 -3
7 1 -1
*/
// 通过下标确定 数字是否出现过
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> res;
for (int i = 0; i < nums.size(); ++i) {
nums[abs(nums[i]) - 1] = -abs(nums[abs(nums[i]) - 1]);
}
for (int j = 0; j < nums.size(); ++j) {
if (nums[j] > 0) {
res.push_back(j + 1);
}
}
return res;
}
};
// @lc code=end