class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
int n = nums.size();
sort(nums.begin(),nums.end());
for(int i = 0; i < n; ++i) {
if(nums[i] > 0) break;
if(i > 0 && nums[i] == nums[i-1]) continue;
int left = i + 1,right = n - 1;
while(left < right) {
int sum = nums[i] + nums[left] +nums[right];
if(sum < 0){
do{++left;} while(nums[left] == nums[left-1] && left < right);
}else if(sum > 0){
do{--right;} while(nums[right] == nums[right+1] && left < right);
}else{
res.push_back({nums[i],nums[left],nums[right]});
do{++left;} while(nums[left] == nums[left-1] && left < right);
do{--right;} while(nums[right] == nums[right+1] && left < right);
}
}
}
return res;
}
};
? ? ? ? 给Krahet大佬跪了,写出来总是考虑不到细节有BUG。