Skip to content

Commit 89425c3

Browse files
authored
Merge pull request gzc426#774 from MMzhe/patch-54
Create sourcema.md
2 parents c7d8d55 + 85e409f commit 89425c3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

2019.01.20-leetcode216/sourcema.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# leetcode 216
2+
class Solution {
3+
public List<List<Integer>> combinationSum3(int k, int n) {
4+
List<List<Integer>> res = new ArrayList<>();
5+
if (k <= 0 || n <= 0) {
6+
return res;
7+
}
8+
find(res, new ArrayList<Integer>(), k, n,1);
9+
return res;
10+
}
11+
private static void find(List<List<Integer>> res, ArrayList<Integer> list, int k, int n,int num) {
12+
if (list.size() == k && n == 0) {
13+
res.add(new ArrayList<Integer>(list));
14+
return;
15+
}
16+
if (list.size() >= k) {
17+
return;
18+
}
19+
for (int i = num; i <=9&&i<=n ; i++) {
20+
list.add(i);
21+
find(res,list,k,n-i,i+1);
22+
list.remove(list.size() - 1);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)