-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKSum.java
More file actions
68 lines (66 loc) · 2.09 KB
/
KSum.java
File metadata and controls
68 lines (66 loc) · 2.09 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package TwoPointer;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class KSum {
public List<List<Integer>> twoSum(int [] nums, int k, int target){
int start = k;
int end = nums.length - 1;
List<List<Integer>> ans = new LinkedList<>();
while(start < end){
if (nums[start] + nums[end] > target || (end < nums.length -1 && nums[end] == nums[end + 1])){
end -= 1;
}
else if (nums[start] + nums[end] < target || (start > k && nums[start] == nums[start-1])){
start += 1;
}
else{
List<Integer> t = new LinkedList<>();
t.add(nums[start]);
t.add(nums[end]);
start ++;
end --;
ans.add(t);
}
}
return ans;
}
public List<List<Integer>> ksum(int [] nums, int start, int target, int k){
List<List<Integer>> ans = new LinkedList<>();
if (start == nums.length){
return ans;
}
int avg = target / k;
if(avg < nums[start] || avg > nums[nums.length-1]){
return ans;
}
if(k == 2){
return twoSum(nums, start, target);
}
for(int i = start; i < nums.length;i ++){
if(i == start || nums[i-1] != nums[i]){
for(List<Integer> res : ksum(nums, i + 1, target - nums[i], k - 1)){
res.add(0, nums[i]);
ans.add(res);
}
}
}
return ans;
}
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
return ksum(nums, 0, target, 4);
}
public static void main(String[] args) {
KSum s = new KSum();
int [] nums = {-2,-1,-1,1,1,2,2};
List<List<Integer>> k = s.fourSum(nums, 0);
for(List ks : k){
for(Object n : ks){
System.out.print(n);
System.out.println(" ");
}
System.out.println();
}
}
}