forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4sum.java
30 lines (30 loc) · 1005 Bytes
/
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
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
Set<List<Integer>> s = new HashSet<>();
List<List<Integer>> output = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
int k = j + 1;
int l = nums.length - 1;
while (k < l) {
long sum = nums[i];
sum += nums[j];
sum += nums[k];
sum += nums[l];
if (sum == target) {
s.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l]));
k++;
l--;
} else if (sum < target) {
k++;
} else {
l--;
}
}
}
}
output.addAll(s);
return output;
}
}