```
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int> > res;
res.clear();
if(num.size()<=2) return res;
sort(num.begin(),num.end());
for (int i=0;i<num.size()-2;i++){
if (i>0 && num[i]==num[i-1]) continue;
int j = i+1,k = num.size()-1;
while(j<k){
if(num[i]+num[j]+num[k]>0){
--k;
}
else if(num[i]+num[j]+num[k]<0){
++j;
}
else{
vector<int> tmp;
tmp.push_back(num[i]);
tmp.push_back(num[j]);
tmp.push_back(num[k]);
res.push_back(tmp);
--k;++j;
}
}
}
return res;
}
};
```