Skip to content

Commit a08eb49

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 251_Flatten_2D_Vector.java
1 parent 6a3b377 commit a08eb49

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Design/251_Flatten_2D_Vector.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Vector2D {
2+
private int[][] v;
3+
private int outer, inner;
4+
5+
public Vector2D(int[][] v) {
6+
this.v = v;
7+
outer = 0;
8+
inner = 0;
9+
}
10+
11+
private void advancedToNext() {
12+
while (outer < v.length && inner == v[outer].length) {
13+
inner = 0;
14+
++outer;
15+
}
16+
}
17+
18+
public int next() {
19+
advancedToNext();
20+
return v[outer][inner++];
21+
}
22+
23+
public boolean hasNext() {
24+
advancedToNext();
25+
return outer < v.length;
26+
}
27+
}

0 commit comments

Comments
 (0)