-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path448. Find All Numbers Disappeared in an Array.cpp
50 lines (50 loc) · 1.4 KB
/
448. Find All Numbers Disappeared in an Array.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
class Solution {
public:
//sorting
vector<int> findDisappearedNumbers1(vector<int>& nums) {
sort(begin(nums),end(nums));
vector<int> res;
int k=0;
for(int i = 1; i<= nums.size();++i)
{
if(k>= nums.size() || nums[k] != i)
res.push_back(i);
while(k<nums.size() && nums[k] == i)
++k;
}
return res;
}
// hasmap
vector<int> findDisappearedNumbers2(vector<int>& nums) {
unordered_set<int> s;
for(int i=0;i<nums.size();++i)
{
s.insert(nums[i]);
}
vector<int> res;
for(int i = 1; i<= nums.size();++i)
{
if(s.count(i))
continue;
res.push_back(i);
}
return res;
}
//inplace
vector<int> findDisappearedNumbers(vector<int>& nums) {
int len = nums.size();
for(int i=0; i<len; i++) {
int m = abs(nums[i])-1; // index start from 0
nums[m] = nums[m]>0 ? -nums[m] : nums[m];
}
vector<int> res;
for(int i = 0; i<len; i++) {
if(nums[i] > 0) res.push_back(i+1);
}
return res;
}
};