题解 | #牛牛的三元组问题#
牛牛的三元组问题
https://www.nowcoder.com/practice/72c6d735fb1144a2ba162976a4510839
该题就是三数之和为模板的题
vector<vector<int> > findTriplets(vector<int>& num) { // write code here vector<vector<int>> res; if(num.size()<3) return res; sort(num.begin(),num.end()); for(int i=0;i<num.size()-2;i++){ if(num[i]==num[i-1] && i) continue; int l=i+1,r=num.size()-1; while (l<r) { if (num[l]+num[r]==-num[i]) { res.push_back({num[i],num[l],num[r]}); while (num[l]==num[l+1] && l+1<r) { l++; } while (num[r]==num[r-1] && r-1>l) { r--; } l++,r--; }else if (num[l]+num[r]>-num[i]) { r--; }else l++; } } return res; } };