-
Notifications
You must be signed in to change notification settings - Fork 8
/
solution.cpp
41 lines (40 loc) · 885 Bytes
/
solution.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
/**
* 28 / 28 test cases passed.
* Status: Accepted
* Runtime: 56 ms
*/
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
for (auto& num : nums) {
nums[(num - 1) % nums.size()] += nums.size();
}
vector<int> ans;
for (int i = 0; i < nums.size(); ++ i) {
if (nums[i] > 2 * nums.size()) {
ans.push_back(i + 1);
}
}
return ans;
}
};
/**
* 28 / 28 test cases passed.
* Status: Accepted
* Runtime: 52 ms
*/
class Solution2 {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> ans;
for (auto& num : nums) {
int n = abs(num);
if (nums[n - 1] < 0) {
ans.push_back(n);
} else {
nums[n - 1] *= -1;
}
}
return ans;
}
};