麻绳先生

做一些记录性的工作

leetcode216

这是因为疫情原因,离开校园,无法获得密钥发布文章的8个多月以来,第一篇文章。
以此测试一下博客环境是否正常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
List<List<Integer>> result = new LinkedList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
backtrace(new LinkedList<Integer>(), 1, k, n, k);
return result;
}
public void backtrace(List<Integer> path, int start, int k, int n, int old){
if(k == 0 && path.size() == old && n == 0){
result.add(new LinkedList<Integer>(path));
return;
}
if(start > 9 || k < 0) return;
for(int i = start; i < 10 && i <= n; ++i){
path.add(i);
backtrace(path, i+1, k-1, n-i, old);
path.remove(path.size()-1);
}
}
}