-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinationSum3.java
More file actions
executable file
·62 lines (48 loc) · 1.47 KB
/
combinationSum3.java
File metadata and controls
executable file
·62 lines (48 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package backtracking;
import array.LIS;
import java.util.ArrayList;
import java.util.List;
/**
* 找出所有可能的 k 个数,使其相加之和为 n,只允许使用数字1-9,并且每一种组合中的数字是唯一的。
示例 1:
输入: k = 3, n = 7
输出:
[[1,2,4]]
示例 2:
输入: k = 3, n = 9
输出:
[[1,2,6], [1,3,5], [2,3,4]]
* Created by Administrator on 2018/4/9 0009.
*/
public class combinationSum3 {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> result=new ArrayList<>();
if(n>k*9||n<k*1)
return result;
List<Integer> list=new ArrayList<>();
backtracking(result,k,n,1,list);
return result;
}
public static void backtracking(List<List<Integer>> result,int k, int n, int start, List<Integer> list){
if(0==k) {
if (n == 0) {
result.add(new ArrayList<>(list));
}
return;
}
if(n<=0){
return;
}
for(int i=start;i<=9;i++){
list.add(i);
backtracking(result,k-1,n-i,i+1,list);
list.remove(list.size()-1);
}
}
public static void main(String[] args) {
combinationSum3 com=new combinationSum3();
List<List<Integer>> result=new ArrayList<>();
result= com.combinationSum3(3,9);
System.out.println(result);
}
}