-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombine.java
More file actions
executable file
·50 lines (44 loc) · 1.28 KB
/
combine.java
File metadata and controls
executable file
·50 lines (44 loc) · 1.28 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
package backtracking;
import array.LIS;
import java.util.ArrayList;
import java.util.List;
/**
* 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
例如,
如果 n = 4 和 k = 2,组合如下:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
* Created by Administrator on 2018/4/8 0008.
*/
public class combine {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> result=new ArrayList<>();
List<Integer> list=new ArrayList<>();
backtracking(result,list,n+1,k,1);
return result;
}
//n 为输入数据 k为结束判断 list为回溯约束 start为回溯后往前更新
public static void backtracking(List<List<Integer>> result,List list,int n,int k,int start){
if(0==k){
result.add(new ArrayList<>(list));
return;
}
for(int i=start;i<n;i++){
list.add(i);
backtracking(result,list,n,k-1,i+1);
list.remove(list.size()-1);
}
}
public static void main(String[] args) {
combine combine=new combine();
List<List<Integer>> result=new ArrayList<>();
result=combine.combine(9,3);
System.out.println(result);
}
}