-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination.java
More file actions
91 lines (80 loc) · 1.36 KB
/
Combination.java
File metadata and controls
91 lines (80 loc) · 1.36 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.Iterator;
import java.util.BitSet;
public class Combination implements Iterator {
private int N;
private BitSet X;
private Object[] items;
private Object[] array;
public Combination(Object[] items, int k) {
this.items = items;
N = items.length;
array = new Object[k];
X = new BitSet(N + 1);
for(int i=0; i<k; i++) X.set(i);
}
public boolean hasNext() {
return ! X.get(N);
}
// 右から見て最初に1となる位置を返す
// オール0なら -1 を返す
// bs = 10011100
// return 2
private int findOne(BitSet bs) {
int len = bs.size();
for(int i=0; i<=N; i++) {
if(bs.get(i)) return i;
}
return -1;
}
// 指定された位置でインクリメントする
// 桁上がりした分の桁数を返す
// bs = 10011100, n = 2
// bs = 10100000
// return 3
private int incr(BitSet bs, int n) {
int a = 0;
for(;;) {
if(bs.get(n)) {
bs.clear(n); n++; a++;
} else {
bs.set(n); break;
}
}
return a;
}
public Object next() {
int k = 0;
for(int i=0; i<=N; i++) {
if(X.get(i)) array[k++] = items[i];
}
int u = incr(X, findOne(X)) - 1;
for(int i=0; i<u; i++) X.set(i);
return array;
}
public void remove(){}
}
/*
N=6
K=3
{1, 2, 3}
{1, 2, 4}
{1, 3, 4}
{2, 3, 4}
{1, 2, 5}
{1, 3, 5}
{2, 3, 5}
{1, 4, 5}
{2, 4, 5}
{3, 4, 5}
{1, 2, 6}
{1, 3, 6}
{2, 3, 6}
{1, 4, 6}
{2, 4, 6}
{3, 4, 6}
{1, 5, 6}
{2, 5, 6}
{3, 5, 6}
{4, 5, 6}
count=20
*/