-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeSum.cpp
More file actions
60 lines (53 loc) · 1.59 KB
/
Copy paththreeSum.cpp
File metadata and controls
60 lines (53 loc) · 1.59 KB
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
51
52
53
54
55
56
57
58
class Solution {
public:
/**
* @param numbers : Give an array numbers of n integer
* @return : Find all unique triplets in the array which gives the sum of zero.
*/
vector<vector<int> > threeSum(vector<int> &nums) {
// write your code here
sort(nums.begin(), nums.end());
vector<vector<int> > res;
for (int i=0; i<nums.size()-2; ++i) {
int j = i+1;
int k = nums.size()-1;
while(j<k){
int sum = nums[i]+nums[j]+nums[k];
if(sum==0){
vector<int> temp;
temp.push_back(nums[i]);
temp.push_back(nums[j]);
temp.push_back(nums[k]);
res.push_back(temp);
j++;
k--;
} else {
if(sum>0){
k--;
}
if(sum<0){
j++;
}
}
}
}
//there are several ways to remove dupilcate in vector:
// 1:
/*
set<vector<int>> s;
for(int i=0; i<res.size(); ++i){
s.insert(res[i]);
}
res.assign(s.begin(), s.end());
*/
// 2:
/*
sort(res.begin(), res.end());
res.erase( unique(res.begin(), res.end()), res.end());
*/
// 3:
set<vector<int>> s(res.begin(), res.end());
res.assign(s.begin(), s.end());
return res;
}
};