-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.cpp
More file actions
29 lines (27 loc) · 706 Bytes
/
Copy path15.cpp
File metadata and controls
29 lines (27 loc) · 706 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
//
// Created by Bright on 2017/1/3.
//
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
vector<vector<int>> threeSum(vector<int>& nums){
int s=nums.size();
vector<vector<int>> ret(0);
if(s<3)
return ret;
sort(nums.begin(),nums.end());
int t1=0,t2=0;
for(int i=0;i<s-2;++i){
if(i>0&&t1==nums[i]) continue;
t1=nums[i];
for(int j=s-1;j>i+1;--j){
if(j<s-1&&t2==nums[j]) continue;
t2=nums[j];
vector<int>::iterator tar;
if((tar=find(nums.begin()+i+1,nums.begin()+j,-t1-t2))!=nums.begin()+j)
ret.push_back({t1,*tar,t2});
}
}
return ret;
}