See More

class Solution { public List> fourSum(int[] nums, int target) { Arrays.sort(nums); Set> s = new HashSet<>(); List> output = new ArrayList<>(); for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { int k = j + 1; int l = nums.length - 1; while (k < l) { long sum = nums[i]; sum += nums[j]; sum += nums[k]; sum += nums[l]; if (sum == target) { s.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l])); k++; l--; } else if (sum < target) { k++; } else { l--; } } } } output.addAll(s); return output; } }