class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> lists = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
if(k<=0 || n<=0 || n<k) return lists;
dfs(lists,path,n,k,1);
return lists;
}
public void dfs(List<List<Integer>> lists,LinkedList<Integer> path,int n,int k,int l){
if(path.size() == k){
int total = getTotal(path);
if(total == n)
lists.add(new ArrayList(path));
return;
}
for(int i = l;i<10;i++){
path.add(i);
dfs(lists,path,n,k,i+1);
path.removeLast();
}
}
public int getTotal(List<Integer> list){
int total = 0;
for(int j : list){
total += j;
}
return total;
}
}
216. 组合总和 III
最新推荐文章于 2024-06-15 20:34:11 发布