class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num int整型vector * @return int整型vector<vector<>> */ vector<vector<int> > threeSum(vector<int>& num) { int i=0; int j=0; int len=num.size(); unordered_map<int,int> hash; sort(num.begin(),num.end()); for(i=0;i<len;i++) { if(hash.count(num[i])==0) { hash[num[i]]=1; } else { hash[num[i]]++; } } vector <vector <int>> ans; for(i=0;i<len-2;i++) { for(j=i+1;j<len-1;j++) { int m=num[i]+num[j]; m=0-m; if(m==num[i] && m==num[j]) { if(hash[m]>=3) { vector <int> b; b.push_back(m); b.push_back(m); b.push_back(m); ans.push_back(b); } } else { if(m==num[i]) { if(hash[m]>=2) { vector <int> b; b.push_back(m); b.push_back(m); b.push_back(num[j]); sort(b.begin(),b.end()); ans.push_back(b); } } else { if(m==num[j]) { if(hash[m]>=2) { vector <int> b; b.push_back(m); b.push_back(m); b.push_back(num[i]); sort(b.begin(),b.end()); ans.push_back(b); } } else { if(hash.count(m)) { vector <int> b; b.push_back(m); b.push_back(num[j]); b.push_back(num[i]); sort(b.begin(),b.end()); ans.push_back(b); } } } } } } sort(ans.begin(),ans.end()); ans.erase(unique(ans.begin(),ans.end()),ans.end()); return ans; } };