/
Combinations.java
34 lines (32 loc) · 1.02 KB
/
Combinations.java
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
import java.util.ArrayList;
import java.util.List;
public class Combinations {
public static List<List<Integer>> combine(int n, int k) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (k == 0 || n == 0) {
return result;
}
combine(1, n, k, result, new ArrayList<Integer>());
return result;
}
private static void combine(int start, int end, int k, List<List<Integer>> result, List<Integer> com) {
if (k == 0) {
result.add(new ArrayList<Integer>(com));
return;
}
for (int i = start; i <= end; i++) {
com.add(i);
combine(i + 1, end, k - 1, result, com);
com.remove(com.size() - 1);
}
}
public static void main(String[] args) {
List<List<Integer>> result = combine(4, 2);
for (List<Integer> t: result) {
for (Integer i: t) {
System.out.print(i + " ");
}
System.out.println("");
}
}
}