leetcode题目链接
1. 题目考点
- dfs 回溯 + 剪枝
画图分析
2. 考点解析
- 数组无重复 + 无重复数字 + 组合无重复
class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
Stack<Integer> path = new Stack<>();
dfs(1, k, n, path, 0);
return res;
}
public void dfs(int start, int k, int n, Stack<Integer> path, int sum) {
if (sum == n && path.size() == k) {
res.add(new ArrayList(path));
return ;
}
if (sum > n) return ;
for (int i = start; i <= 9; i++) {
sum += i;
path.push(i);
dfs(i+1, k, n, path, sum);
sum -= i;
path.pop();
}
}
}