cpp
class Solution {
public:
void paixu(int l,int r,vector<int>& nums){
int mid=l+(r-l)/2;
paixu(l,mid-1,nums);
paixu(mid,r,nums);
merge(l,r,nums);
}
void merge(int l,int r,vector<int>& nums){
vector<int> tmp;
int mid=l+(r-l)/2;
int p=mid;
int start=l;
while(l<=mid && p<=r){
if(nums[l]<nums[p]){
tmp.push_back(nums[l]);
l+=1;
}
else{
tmp.push_back(nums[p]);
p+=1;
}
}
while(l<=mid)
tmp.push_back(nums[l++]);
while(p<=r)
tmp.push_back(nums[p++]);
for(int i=0;i<tmp.size();i++){
nums[start+i]=tmp[i];
}
}
vector<vector<int>> threeSum(vector<int>& nums) {
// 先排序 在计算和
int l=0;
int r=nums.size()-1;
// paixu(l,r,nums);
sort(nums.begin(),nums.end());
vector<vector<int>> ans;
int a=0;
while(a<nums.size()-2 && nums[a]<=0){
int b=a+1;
int c=nums.size()-1;
while(b<c){
if(nums[a] + nums[b] + nums[c] < 0) b++;
else if(nums[a] + nums[b] + nums[c] > 0) c--;
else{
ans.push_back(vector<int>{nums[a] ,nums[b] ,nums[c]});
b++;
c--;
}
//去重
while(b>a+1 && b<nums.size()-1 &&nums[b-1]==nums[b]){
b+=1;
}
while(c>=1 && c<nums.size()-1 &&nums[c+1]==nums[c]){
c-=1;
}
}
a+=1;
while(a>=1 && a<nums.size()-1 && nums[a-1]==nums[a]){
a+=1;
}
}
return ans;
}
};