-
Notifications
You must be signed in to change notification settings - Fork 15
/
k-sum-ii.java
44 lines (39 loc) · 1.4 KB
/
k-sum-ii.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
39
40
41
42
43
public class Solution {
/**
* @param A: an integer array.
* @param k: a positive integer (k <= length(A))
* @param target: a integer
* @return a list of lists of integer
*/
public ArrayList<ArrayList<Integer>> kSumII(int A[], int k, int target) {
// write your code here
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
int len = A.length;
if (len == 0) {
return res;
}
ArrayList<Integer> tempList = new ArrayList<Integer>();
for (int i = 0; i < len; i++) {
tempList.add(A[i]);
dfsHelper(A, res, tempList, target, i, k, A[i]);
tempList.remove(0);
}
return res;
}
public void dfsHelper(int[] A, ArrayList<ArrayList<Integer>> res, ArrayList<Integer>
tempList, int target, int index, int k, int curSum) {
if (tempList.size() == k) {
if (curSum == target) {
res.add(new ArrayList<Integer>(tempList));
}
return;
}
for (int i = index + 1; i < A.length; i++) {
tempList.add(A[i]);
curSum += A[i];
dfsHelper(A, res, tempList, target, i, k, curSum);
curSum -= A[i];
tempList.remove(tempList.size() - 1);
}
}
}