-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination-sum-iii.java
More file actions
43 lines (35 loc) · 1.22 KB
/
combination-sum-iii.java
File metadata and controls
43 lines (35 loc) · 1.22 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
class Solution {
private int maxSize;
private int maxSum;
private final boolean[] used = new boolean[10];
private List<List<Integer>> answer;
private int[] selected;
public List<List<Integer>> combinationSum3(int k, int n) {
maxSize = k;
maxSum = n;
answer = new ArrayList<>();
selected = new int[k];
combination(0, 0, 1, new LinkedList<>());
return answer;
}
private void combination(int curSize, int curSum, int start, LinkedList<Integer> curList) {
if (curSize == maxSize && curSum == maxSum) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < maxSize; i++) {
list.add(selected[i]);
}
answer.add(list);
return;
} else if (curSize == maxSize || curSum > maxSum) {
return;
}
for (int i = start; i < 10; i++) {
if (used[i]) continue;
used[i] = true;
selected[curSize] = i;
combination(curSize + 1, curSum + i, i + 1, curList);
selected[curSize] = -1;
used[i] = false;
}
}
}