-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.java
More file actions
74 lines (67 loc) · 1.98 KB
/
Combinations.java
File metadata and controls
74 lines (67 loc) · 1.98 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
63
64
65
66
67
68
69
70
71
72
73
import java.util.ArrayList;
public class Combinations {
/*
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (n < k || k <= 0) {
return result;
}
result.add(new ArrayList<Integer>());
for (int i = 0; i < k; i++) {
ArrayList<ArrayList<Integer>> temp = new ArrayList<ArrayList<Integer>>();
for (ArrayList<Integer> arr : result) {
int prevLen = arr.size();
int start = 0;
if (prevLen > 0) {
start = arr.get(prevLen - 1);
}
for (int j = start + 1; j <= n - k + 1 + i; j++) {
ArrayList<Integer> a = new ArrayList<Integer>(arr);
a.add(j);
temp.add(a);
}
}
result = temp;
}
return result;
}
*/
long count = 0;
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (n < k || k <= 0) {
return result;
}
result = recur(1, n, k);
System.out.println("leetcode : count = " + count);
System.out.println("result.size = " + result.size());
return result;
}
public ArrayList<ArrayList<Integer>> recur(int start, int n, int k) {
count++;
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (k == 1) {
for (int i = start; i <= n; i++) {
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(i);
result.add(arr);
}
return result;
}
for (int i = start; i + k - 1 <= n; i++) {
ArrayList<ArrayList<Integer>> temp = recur(i + 1, n, k - 1);
for (ArrayList<Integer> arr: temp) {
arr.add(0,i);
result.add(arr);
}
}
return result;
}
public void test() {
long startTime = System.currentTimeMillis();
combine(10,5);
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println(totalTime);
}
}