-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ77Combinations.java
More file actions
37 lines (33 loc) · 989 Bytes
/
Q77Combinations.java
File metadata and controls
37 lines (33 loc) · 989 Bytes
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
import java.util.ArrayList;
import java.util.List;
/**
* 77 Combinations
* @author ahscuml
* @date 2018/9/15
* @time 10:42
*/
public class Q77Combinations {
public static void main(String[] args) {
int n = 4, k = 2;
System.out.println(combine(n, k).toString());
}
public static List<List<Integer>> combine(int n, int k) {
List<List<Integer>> list = new ArrayList();
backTracking(list, new ArrayList(), n, k, 1);
return list;
}
public static void backTracking(List<List<Integer>> list, List<Integer> templist, int n, int k, int start){
if(templist.size() == k) {
list.add(new ArrayList(templist));
return;
}
for(int i = start; i <= n; i++){
if(templist.contains(i)) {
continue;
}
templist.add(i);
backTracking(list, templist, n, k, i + 1);
templist.remove(templist.size() - 1);
}
}
}