Skip to content

Commit 1470dbc

Browse files
authored
Create 442. Find All Duplicates in an Array (#439)
2 parents 47f79af + cc71ca7 commit 1470dbc

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

442. Find All Duplicates in an Array

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
vector<int> findDuplicates(vector<int>& nums) {
4+
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
5+
6+
int n = nums.size();
7+
nums.push_back(0);
8+
vector<int> res;
9+
for (int i = 0; i <= n ; ++i)
10+
{
11+
while(nums[i] != nums[nums[i]])
12+
swap(nums[i], nums[nums[i]]);
13+
}
14+
for (int i = 0; i <= n; ++i)
15+
{
16+
if (nums[i] != i)
17+
res.push_back(nums[i]);
18+
}
19+
20+
21+
return res;
22+
23+
}
24+
};

0 commit comments

Comments
 (0)