-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_sum_zero.cpp
More file actions
39 lines (39 loc) · 931 Bytes
/
3_sum_zero.cpp
File metadata and controls
39 lines (39 loc) · 931 Bytes
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
vector<vector<int> > Solution::threeSum(vector<int> &A) {
int B = 0;
sort(A.begin(),A.end());
int n = A.size();
vector <vector<int>> res;
if (n<3)
return res;
int ans = 0;
if (n==3){
for(int x:A)
ans+=x;
if (ans==0)
res.emplace_back(A);
return res;
}
map<vector<int>,bool> m;
for(int i=0;i<n;i++){
// cout<<i<<endl;
int j = i+1;
int k = n-1;
while (j<k){
if (A[i]+A[j]+A[k]==B){
vector <int> tmp(3);
tmp[0] = A[i];
tmp[1] = A[j];
tmp[2] = A[k];
if (m.find(tmp)==m.end()){
res.emplace_back(tmp);
m[tmp] = true;
}
}
if ((A[i]+A[j]+A[k])<B)
j++;
else
k--;
}
}
return res;
}