-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_216.java
More file actions
31 lines (26 loc) · 748 Bytes
/
Copy pathP_216.java
File metadata and controls
31 lines (26 loc) · 748 Bytes
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
package leetcode.medium;
import java.util.ArrayList;
import java.util.List;
public class P_216 {
static List<List<Integer>> res;
static List<Integer> curr;
public List<List<Integer>> combinationSum3(int k, int n) {
res = new ArrayList<>();
curr = new ArrayList<>();
dfs(1, n, k);
return res;
}
private static void dfs(int idx, int target, int k) {
if (k == 0) {
if (target == 0) {
res.add(new ArrayList<>(curr));
}
return;
}
for (int num = idx; num < 10 && num <= target; num++) {
curr.add(num);
dfs(num + 1, target - num, k - 1);
curr.remove(curr.size() - 1);
}
}
}