classSolution{ public List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> ans = new ArrayList<>(); int size = nums.length; if (size < 4) return ans; Arrays.sort(nums); for (int a = 0; a <= size- 4; a ++) { if (a > 0 && nums[a] == nums[a - 1]) continue; //去重 for (int b = a + 1; b <= size - 3; b ++) { if (b > a + 1 && nums[b] == nums[b - 1]) continue; //去重 int c = b + 1; int d = size - 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 { List<Integer> tem = new ArrayList<>(); tem.add(nums[a]); tem.add(nums[b]); tem.add(nums[c]); tem.add(nums[d]); ans.add(tem); while (c < d && nums[c + 1] == nums[c]) c++; //去重 while (c < d && nums[d - 1] == nums[d]) d--; //去重 c++; d--; } } } } return ans; } }