-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCombination_216.java
30 lines (29 loc) · 1007 Bytes
/
Combination_216.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Combination_216 {
public static List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<>();
helper(res, new ArrayList<>(), 1, k, n);
return res;
}
private static void helper(List<List<Integer>> ls, List<Integer> tempList, int start, int k, int target) {
if (tempList.size() == k) {
if (target == 0)
ls.add(new ArrayList<>(tempList));
}
else {
for (int i=start; i<=Math.min(target, 9); i++) {
tempList.add(i);
helper(ls, tempList, i+1, k, target-i);
tempList.remove(tempList.size()-1);
}
}
}
public static void main(String[] args) {
List<List<Integer>> res = combinationSum3(3,9);
for (List<Integer> i:res) {
System.out.println(Arrays.toString(i.toArray()));
}
}
}