class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for(int i=0;i<nums.length-3;i++){//四数之和
int target1 = target - nums[i];
if((i>0&&nums[i]==nums[i-1])) continue;
for(int j=i+1;j<nums.length-2;j++){//转为三数之和
if((j>i+1&&nums[j]==nums[j-1])) continue;
int l = j+1,r=nums.length-1;
while (l < r){//两数之和模板
int sum = nums[j]+nums[l]+nums[r];
if(sum == target1){
res.add(Arrays.asList(nums[i],nums[j],nums[l],nums[r]));
while(l < r && nums[r] == nums[r-1]) r--;
while (l < r && nums[l] == nums[l+1]) l++;
l++;r--;
}
else if(sum < target1) l++;
else r--;
}
}
}
return res;
}
}