题解 | #数组中相加和为0的三元组#
数组中相加和为0的三元组
http://www.nowcoder.com/practice/345e2ed5f81d4017bbb8cc6055b0b711
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
set<vector<int>>s;
int n=num.size();
if(n<3)
{
return {};
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
int t=0-num[i]-num[j];
vector<int>temp;
auto it=find(num.begin()+j+1,num.end(),t);
if(it!=num.end())
{
temp.push_back(num[i]);
temp.push_back(num[j]);
temp.push_back(*it);
sort(temp.begin(),temp.end());
s.insert(temp);
}
}
}
vector<vector<int>>ans;
for(auto v:s)
{
ans.push_back(v);
}
return ans;
}
};</int></int></int></int></int>