Skip to content

Commit eb404b8

Browse files
Sean PrashadSean Prashad
authored andcommitted
Update 216_Combination_Sum_III.java
1 parent c8e6bd9 commit eb404b8

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
class Solution {
22
public List<List<Integer>> combinationSum3(int k, int n) {
33
List<List<Integer>> result = new ArrayList<>();
4-
dfs(k, n, 1, new ArrayList<>(), result);
4+
helper(n, k, 1, new ArrayList<>(), result);
55
return result;
66
}
77

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));
8+
private void helper(int n, int numsAllowed, int start, List<Integer> temp, List<List<Integer>> result) {
9+
if (numsAllowed == 0 && n == 0) {
10+
result.add(new ArrayList<>(temp));
1111
return;
1212
}
1313

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);
14+
for (int i = start; i <= 9; i++) {
15+
temp.add(i);
16+
helper(n - i, numsAllowed - 1, i + 1, temp, result);
17+
temp.remove(temp.size() - 1);
1818
}
1919
}
2020
}

0 commit comments

Comments
 (0)