class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int>>*v =new vector<vector<int>>();
if(num.size()<3)
{
return *v;
}
sort(num.begin(),num.end());
for(int i=0;i<num.size()-2;i++)
{
if(i>0&&num[i]==num[i-1])
continue;
for(int j=i+1;j<num.size()-1;j++)
{
if(j>i+1&&num[j]==num[j-1])
continue;
int left=j+1;
int right=num.size()-1;
while(left<=right)
{
int middle=(left+right)/2;
if(num[i]+num[j]+num[middle]==0)
{
vector<int> vec;
vec.push_back(num[i]);
vec.push_back(num[j]);
vec.push_back(num[middle]);
v->push_back(vec);
break;
}
else if(num[i]+num[j]+num[middle]>0)
{
right=middle-1;
}else
{
left=middle+1;
}
}
}
}
return *v;
}
};
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int>>*v =new vector<vector<int>>();
if(num.size()<3)
{
return *v;
}
sort(num.begin(),num.end());
for(int i=0;i<num.size()-2;i++)
{
if(i>0&&num[i]==num[i-1])
continue;
for(int j=i+1;j<num.size()-1;j++)
{
if(j>i+1&&num[j]==num[j-1])
continue;
int left=j+1;
int right=num.size()-1;
while(left<=right)
{
int middle=(left+right)/2;
if(num[i]+num[j]+num[middle]==0)
{
vector<int> vec;
vec.push_back(num[i]);
vec.push_back(num[j]);
vec.push_back(num[middle]);
v->push_back(vec);
break;
}
else if(num[i]+num[j]+num[middle]>0)
{
right=middle-1;
}else
{
left=middle+1;
}
}
}
}
return *v;
}
};