-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
32 lines (30 loc) · 961 Bytes
/
Copy pathSolution.java
File metadata and controls
32 lines (30 loc) · 961 Bytes
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
public class Solution
{
public List<List<Integer>> fourSum(int[] nums, int target)
{
Arrays.sort(nums);
Set<List<Integer>> result = new HashSet<List<Integer>>();
for (int i = 0; i < nums.length - 3; i++)
{
threeSum(i, nums, target, result);
}
return new ArrayList<>(result);
}
private void threeSum(int idx, int[] nums, int target, Set<List<Integer>> result)
{
for (int i = idx + 1; i < nums.length - 2; i++)
{
int j = i + 1, k = nums.length - 1;
while (j < k)
{
int sum = nums[i] + nums[j] + nums[k] + nums[idx] - target;
if (sum == 0)
{
result.add(Arrays.asList(nums[idx], nums[i], nums[j], nums[k]));
}
j = sum < 0 || sum == 0 ? j + 1 : j;
k = sum > 0 || sum == 0 ? k - 1 : k;
}
}
}
}