-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path15.cpp
More file actions
executable file
·48 lines (47 loc) · 1.4 KB
/
15.cpp
File metadata and controls
executable file
·48 lines (47 loc) · 1.4 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
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
static vector<vector<int> > threeSum(vector<int>& nums) {
vector<vector<int> > resVec;
sort(nums.begin(),nums.end());
for(int p1=0;p1<nums.size();p1++){
if(nums[p1] >0) // if > 0 that means no answer
return resVec;
if(p1>0 && nums[p1] == nums[p1-1])
continue;
int p2 = p1+1;//begin
int p3 = nums.size()-1;//end
while(p2<p3){
int a = nums[p1];
int b = nums[p2];
int c = nums[p3];
if(b+c== -a){
vector<int> tempVec(3);
tempVec[0] = a;
tempVec[1] = b;
tempVec[2] = c;
resVec.push_back(tempVec);
while (p2 < p3 && nums[p2] == nums[p2 + 1]) //去除重复元素
p2++;
while (p2 < p3 && nums[p3 - 1] == nums[p3]) //去除重复元素
p3--;
p2++;
p3--;
}
else if(b+c < -a){
p2++;
}
else{
p3--;
}
}
}
return resVec;
}
};
int main(){
}