Given an array nums
of n
integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]]
such that:
0 <= a, b, c, d < n
a
,b
,c
, andd
are distinct.nums[a] + nums[b] + nums[c] + nums[d] == target
You may return the answer in any order.
Example 1:
Input: nums = [1,0,-1,0,-2,2], target = 0 Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2:
Input: nums = [2,2,2,2,2], target = 8 Output: [[2,2,2,2]]
Constraints:
1 <= nums.length <= 200
-109 <= nums[i] <= 109
-109 <= target <= 109
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
res = []
if nums is None or len(nums) < 4:
return res
n = len(nums)
nums.sort()
for i in range(n - 3):
if i > 0 and nums[i] == nums[i - 1]:
continue
for j in range(i + 1, n - 2):
if j > i + 1 and nums[j] == nums[j - 1]:
continue
p, q = j + 1, n - 1
while p < q:
if p > j + 1 and nums[p] == nums[p - 1]:
p += 1
continue
if q < n - 1 and nums[q] == nums[q + 1]:
q -= 1
continue
t = nums[i] + nums[j] + nums[p] + nums[q]
if t == target:
res.append([nums[i], nums[j], nums[p], nums[q]])
p += 1
q -= 1
elif t < target:
p += 1
else:
q -= 1
return res
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
int n;
if (nums == null || (n = (nums.length)) < 4) {
return Collections.emptyList();
}
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < n - 3; ++i) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < n - 2; ++j) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
int p = j + 1, q = n - 1;
while (p < q) {
if (p > j + 1 && nums[p] == nums[p - 1]) {
++p;
continue;
}
if (q < n - 1 && nums[q] == nums[q + 1]) {
--q;
continue;
}
int t = nums[i] + nums[j] + nums[p] + nums[q];
if (t == target) {
res.add(Arrays.asList(nums[i], nums[j], nums[p], nums[q]));
++p;
--q;
} else if (t < target) {
++p;
} else {
--q;
}
}
}
}
return res;
}
}