Skip to content

Commit 0752841

Browse files
committed
Implement Iterable for Permutation
1 parent 1ef3510 commit 0752841

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

Permutation/Permutation.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
class Permutation implements java.util.Iterator<int[]> {
1+
/*
2+
* Verified
3+
* https://atcoder.jp/contests/abc054/submissions/16977824
4+
*/
5+
class Permutation implements java.util.Iterator<int[]>, Iterable<int[]> {
26
private int[] next;
37

48
public Permutation(int n) {
@@ -17,6 +21,11 @@ public int[] next() {
1721
return r;
1822
}
1923

24+
@Override
25+
public java.util.Iterator<int[]> iterator() {
26+
return this;
27+
}
28+
2029
public static int[] nextPermutation(int[] a) {
2130
if (a == null || a.length < 2)
2231
return null;

Permutation/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
$N$が与えられたとき,長さ$N$の順列を辞書順に列挙することができます.
66

7+
また,拡張 for 文によるイテレーションをサポートしています.
8+
以下のイテレーションは,時間計算量 $O(N * N!)$で動作します.
9+
10+
```
11+
Permutation perm = new Permutation(n);
12+
for (int[] p : perm) {
13+
// code here
14+
}
15+
```
16+
717
## コンストラクタ
818

919
### Permutation
@@ -32,6 +42,14 @@ public int[] next()
3242

3343
イテレータの後続の要素を取得します.計算量 $O(N)$
3444

45+
### iterator
46+
47+
```java
48+
public Iterator<int[]> iterator()
49+
```
50+
51+
順列を列挙するイテレータを取得します.
52+
3553
### nextPermutation
3654

3755
```java

0 commit comments

Comments
 (0)