-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
442.cpp
45 lines (40 loc) · 1.29 KB
/
442.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
__________________________________________________________________________________________________
sample 72 ms submission
static int x = [](){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
int len = nums.size();
vector<int> res;
for(int i=0; i<len; i++) {
int m = abs(nums[i])-1; // index start from 0
nums[m] = -nums[m];
if(nums[m] > 0) res.push_back(m+1);
}
return res;
}
};
__________________________________________________________________________________________________
sample 14416 kb submission
static int fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
class Solution {
public:
std::vector<int> findDuplicates(std::vector<int>& nums) {
std::vector<int> res;
res.reserve(nums.size() / 2);
std::vector<bool> seen(nums.size() + 1, false);
for (int num : nums) {
if (seen[num]) {
res.push_back(num);
}
seen[num] = true;
}
return res;
}
};
__________________________________________________________________________________________________