https://leetcode.com/problems/4sum/
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>>res;
if(nums.size()<4) return res;
sort(nums.begin(), nums.end());
for(int fir=0;fir<nums.size()-3;fir++){
if(fir>0&&nums[fir]==nums[fir-1]) continue; //防止重复
for(int sec=fir+1;sec<nums.size()-2;sec++){
if(sec>fir+1&&nums[sec]==nums[sec-1]) continue;
int flag=target-nums[fir]-nums[sec];
int i=sec+1, j=nums.size()-1;
while (i<j) {
if(nums[i]+nums[j]==flag){
res.push_back({nums[fir],nums[sec],nums[i],nums[j]});
while (i<j&&nums[i]==nums[i+1]) i+=1; //去重
while (i<j&&nums[j]==nums[j-1]) j-=1; //去重
i+=1; j-=1;
}
else if(nums[i]+nums[j]<flag) i+=1;
else j-=1;
}
}
}
return res;
}
};