文章目录
- 题目介绍
- 解法
题目介绍
解法
是77.组合链接的扩展
class Solution {
List<List<Integer>> result= new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum3(int n, int k) {
dfs(n, k, 1, 0);
return result;
}
public void dfs(int targetSum, int k, int startIndex, int sum){
if (path.size() == k){
if (sum == targetSum){
result.add(new ArrayList<>(path));
}
return;
}
for (int i = startIndex;i <= 9;i++){
path.add(i);
sum += i;
dfs(targetSum, k, i + 1, sum);
path.remove(path.size() - 1);
sum -= i;
}
}
}