-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombine.java
More file actions
45 lines (39 loc) · 1.19 KB
/
Combine.java
File metadata and controls
45 lines (39 loc) · 1.19 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
package combinations;
import java.util.ArrayList;
import java.util.List;
public class Combine {
public List<List<Integer>> combine(int n, int k){
List<List<Integer>> res = new ArrayList<>();
if (k > n || n == 0 || k == 0){
return null;
}
else {
back(1, n, k, res, new ArrayList<>());
}
return res;
}
public void back(int begin, int n, int k, List<List<Integer>> res, List<Integer> list){
if (k == 0){
List<Integer> tmp = new ArrayList<>(list);
res.add(tmp);
}
else {
for (int i = begin; i <= n-k+1; i++){
list.add(i);
back(i+1, n, k-1, res, list);
list.remove(list.size()-1);
}
}
}
public static void main(String[] args){
Combine t = new Combine();
int n = 4, k = 0;
List<List<Integer>> res = t.combine(n, k);
for (int i = 0; i < res.size(); i++){
for (int j = 0; j < k; j++){
System.out.print(res.get(i).get(j)+",");
}
System.out.println();
}
}
}