0%

Leetcode018-4Sum

Description

Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example

1
2
3
4
5
6
7
8
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
int i = 0;
Arrays.sort(nums);
while (i < nums.length - 3){
int j = i + 1;
while (j < nums.length - 2){
List<List<Integer>> tmp = twoSum(nums, j + 1, target-nums[i]-nums[j]);
if (tmp.size() > 0){
for (List<Integer> t : tmp){
t.add(nums[i]);
t.add(nums[j]);
res.add(t);
}
}
j ++;
while (j < nums.length - 2 && nums[j] == nums[j-1]) j++;
}
i++;
while(i < nums.length - 3 && nums[i] == nums[i-1]) i++;
}
return res;
}
private List<List<Integer>> twoSum(int[] nums, int i, int target){
int j = nums.length - 1;
List<List<Integer>> res = new ArrayList<>();
while (i < j){
if (nums[i] + nums[j] == target){
List<Integer> tmp = new ArrayList<>();
tmp.add(nums[i]);
tmp.add(nums[j]);
res.add(tmp);

i++;
j--;
while(i < j && nums[i] == nums[i-1]) i++;
while(i < j && nums[j] == nums[j+1]) j--;
}
else if (nums[i] + nums[j] < target) i++;
else j--;
}
return res;
}
}