-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLC_15.cpp
38 lines (36 loc) · 1001 Bytes
/
LC_15.cpp
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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>>res;
int i,j,sum,low,high,sz=nums.size();
map<vector<int>,int>mp;
sort(nums.begin(),nums.end());
if((sz<3) || (nums[0]>0) || (nums[sz-1]<0))
return res;
for(i=0;i<sz-2;i++)
{
low=i+1;
high=sz-1;
while(low<high)
{
sum=nums[i]+nums[low]+nums[high];
if(sum>0)
high-=1;
else if(sum<0)
low+=1;
else if(sum==0)
{
vector<int>v={nums[i],nums[low],nums[high]};
if(mp.find(v)==mp.end())
{
res.push_back(v);
mp[v]=true;
}
low++;
high--;
}
}
}
return res;
}
};