-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.4sum.java
More file actions
47 lines (39 loc) · 1.56 KB
/
Copy path18.4sum.java
File metadata and controls
47 lines (39 loc) · 1.56 KB
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
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(nums.length < 4)
return res;
Arrays.sort(nums);
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
int l = j+1;
int h = nums.length-1;
while(l<h){
if(nums[i]+nums[j]+nums[l]+nums[h] == target){
ArrayList<Integer> curr = new ArrayList<Integer>();
curr.add(nums[i]);
curr.add(nums[j]);
curr.add(nums[l]);
curr.add(nums[h]);
res.add(curr);
int l_f_val = nums[l];
while(l<h && nums[l] == l_f_val)
l++;
int r_g_val = nums[h];
while(l<h && nums[h] == r_g_val)
h--;
}
else if(nums[i]+nums[j]+nums[l]+nums[h] < target)
l++;
else
h--;
}
while(j<nums.length-1 && nums[j] == nums[j+1])
j++;
}
while(i<nums.length-1 && nums[i] == nums[i+1])
i++;
}
return res;
}
}