LeetCode 216. 组合总和 III
题目描述
思路分析
思路描述
参考代码
1
write your code here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), n, k, 1);
return res;
}
public void backtrack(List<List<Integer>> res, List<Integer> path, int pathSum, int k, int start) {
if (path.size() == k && pathSum == 0) {
res.add(new ArrayList<>(path));
return;
} else if (path.size() == k || pathSum < 0) {
return;
}
for (int i = start; i <= 9; i++) {
path.add(i);
backtrack(res, path, pathSum - i, k, i + 1);
path.remove(path.size() - 1);
}
}
}
CC BY-NC-SA 4.0
许可协议,转载请注明出处!
本博客所有文章除特别声明外,均采用