Skip to content

Commit 089d568

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 216_Combination_Sum_III.java
1 parent 864c3dd commit 089d568

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public List<List<Integer>> combinationSum3(int k, int n) {
3+
List<List<Integer>> result = new ArrayList<>();
4+
dfs(k, n, 1, new ArrayList<>(), result);
5+
return result;
6+
}
7+
8+
private void dfs(int k, int n, int idx, List<Integer> tempResult, List<List<Integer>> result) {
9+
if (tempResult.size() == k && n == 0) {
10+
result.add(new ArrayList<>(tempResult));
11+
return;
12+
}
13+
14+
for (int i = idx; i <= 9; i++) {
15+
tempResult.add(i);
16+
dfs(k, n - i, i + 1, tempResult, result);
17+
tempResult.remove(tempResult.size() - 1);
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)