-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.cpp
More file actions
47 lines (38 loc) · 1.15 KB
/
3Sum.cpp
File metadata and controls
47 lines (38 loc) · 1.15 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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> Sol;
sort(nums.begin(),nums.end());
set<vector<int>> s;
set<int> elements;
for(int i=0;i<nums.size();i++){
int a=nums[i];
if(elements.find(a)!=elements.end())
continue;
elements.insert(a);
int low=i+1, high=nums.size()-1;
while(low<high){
int sum=a+nums[low]+nums[high];
if(sum==0){
vector<int>ans;
ans.push_back(a);
ans.push_back(nums[low]);
ans.push_back(nums[high]);
if(s.find(ans)==s.end())
{
Sol.push_back(ans);
s.insert(ans);
}
low++;
high--;
}
else if(sum>0){
high--;
}
else
low++;
}
}
return Sol;
}
};