classSolution{public List<List<Integer>>fourSum(int[] nums,int target){
List<List<Integer>> res =newArrayList();
Arrays.sort(nums);int a, b, c, d;int n = nums.length;if(n <4)return res;for(a =0; a <= n -4; a ++){if(a >0&& nums[a -1]== nums[a]){continue;}for(b = a +1; b <= n -3; b ++){if(b > a +1&& nums[b -1]== nums[b]){continue;}
c = b +1;
d = n -1;while(c < d){if(nums[a]+ nums[b]+ nums[c]+ nums[d]< target){
c ++;}elseif(nums[a]+ nums[b]+ nums[c]+ nums[d]> target){
d --;}else{
res.add(Arrays.asList(nums[a],nums[b],nums[c],nums[d]));while(c < d && nums[c +1]== nums[c]){
c ++;}while(c < d && nums[d -1]== nums[d]){
d --;}
c ++;
d --;}}}}return res;}}