class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
List<Integer> path = new ArrayList<>();
backtrack(1, k, n, path);
return res;
}
private void backtrack(int id, int k, int n, List<Integer> path){
if (n == 0 && k == 0){
res.add(new ArrayList<>(path));
return;
}
if (n < 0 && k == 0){
return;
}
for (int i = id; i < 10; i++) {
if (i > n) continue;
n -= i;
path.add(i);
backtrack(i + 1, k - 1, n, path);
path.remove(path.size() - 1);
n += i;
}
}
}
leetcode216组合总和 III
猜你喜欢
转载自blog.csdn.net/weixin_50070650/article/details/112243671
今日推荐
周排行