-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_18.java
More file actions
45 lines (37 loc) · 1.45 KB
/
Copy pathleetCode_18.java
File metadata and controls
45 lines (37 loc) · 1.45 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
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
return KSum(0, 4, nums, target);
}
public List<List<Integer>> KSum(int start, int k, int[]nums, int target){
List<List<Integer>> result = new ArrayList<List<Integer>>();
if(k == 2){
int left = start, right = nums.length-1;
while(left < right){
int sum = nums[left] + nums[right];
if(sum == target){
List<Integer> ll = new ArrayList<>();
ll.add(nums[left]);
ll.add(nums[right]);
result.add(ll);
while(left < right && nums[left] == nums[left+1]) left++;
while(left < right && nums[right] == nums[right-1]) right--;
left++;
right--;
}
else if(sum < target) left++;
else {right--;}
}
return result;
}
for(int i=start; i < nums.length - (k-1); i++){
if(i > start && nums[i] == nums[i-1]) continue;
List<List<Integer>> ll = KSum(i+1, k-1, nums, target-nums[i]);
for(List<Integer> list: ll){
list.add(0, nums[i]);
}
result.addAll(ll);
}
return result;
}
}