题目链接:https://leetcode.cn/problems/combination-sum-iii/
方法一 回溯
1 方法思想
2 代码实现
class Solution {
List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
backTracking(1, 0, k, n);
return result;
}
public void backTracking(int start,int sum, int num, int target){
if (path.size() >= num){
if (sum == target){
result.add(new ArrayList<>(path));
}
return;
}
for (int i = start; i < 10; i++) {
path.add(i);
backTracking(i + 1, sum + i, num, target);
path.removeLast();
}
}
}
3 复杂度分析
时间复杂度:
空间复杂度: