-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18. 4Sum.java
38 lines (38 loc) · 1.42 KB
/
18. 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
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
int n = nums.length;
List<List<Integer>> result = new ArrayList<>();
Map<List<Integer>, Integer> hmap = new HashMap<>();
for(int i = 0; i < n - 3; i++){
for(int j = i + 1; j < n - 2; j++){
// if(nums[j] == nums[j - 1]) continue;
int left = j + 1;
int right = n - 1;
long res = (nums[i] + nums[j]);
// System.out.println(res);
long remSum = target - res;
// System.out.println(remSum);
while(left < right){
long twoSum = nums[left] + nums[right];
if(twoSum > remSum) right--;
else if(twoSum < remSum) left++;
else{
List<Integer> li = new ArrayList<>();
li.add(nums[i]);
li.add(nums[j]);
li.add(nums[left]);
li.add(nums[right]);
int count = hmap.getOrDefault(li, 0);
if(count == 0){
result.add(li);
hmap.put(li,++count);
}
left++;
}
}
}
}
return result;
}
}