-
Notifications
You must be signed in to change notification settings - Fork 0
/
4Sum.java
53 lines (48 loc) · 1.89 KB
/
4Sum.java
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
46
47
48
49
50
51
52
53
/*
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets
in the array which gives the sum of target.
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
Link: https://leetcode.com/problems/4sum/
Example: For example, given array S = {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: None
Source: None
*/
public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
HashSet<List<Integer>> hashSet = new HashSet<List<Integer>>();
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(nums);
for (int i = 0; i <= nums.length-4; i++) {
for (int j = i + 1; j <= nums.length-3; j++) {
int low = j + 1;
int high = nums.length - 1;
while (low < high) {
int sum = nums[i] + nums[j] + nums[low] + nums[high];
if (sum > target) {
high--;
} else if (sum < target) {
low++;
} else if (sum == target) {
List<Integer> temp = new ArrayList<Integer>();
temp.add(nums[i]);
temp.add(nums[j]);
temp.add(nums[low]);
temp.add(nums[high]);
if (!hashSet.contains(temp)) {
hashSet.add(temp);
result.add(temp);
}
low++;
high--;
}
}
}
}
return result;
}
}