forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4Sum.java
More file actions
25 lines (25 loc) · 741 Bytes
/
4Sum.java
File metadata and controls
25 lines (25 loc) · 741 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
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
Set<List<Integer>> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
int newTarget = target - nums[i] - nums[j];
int start = j + 1;
int end = nums.length - 1;
while (start < end) {
if ((nums[start] + nums[end]) == newTarget) {
set.add(Arrays.asList(nums[i], nums[j], nums[start++], nums[end--]));
}
else if ((nums[start] + nums[end]) < newTarget) {
start++;
}
else {
end--;
}
}
}
}
return new ArrayList<>(set);
}
}