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