forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path251_Vector2D.java
42 lines (38 loc) · 997 Bytes
/
251_Vector2D.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
35
36
37
38
39
40
41
42
public class Vector2D implements Iterator<Integer> {
List<List<Integer>> vec2d;
int x;
int y;
public Vector2D(List<List<Integer>> vec2d) {
this.vec2d = vec2d;
this.x = 0;
this.y = 0;
while(x < vec2d.size() && (vec2d.get(x) == null || vec2d.get(x).size() == 0)){
x++;
}
}
@Override
public Integer next() {
List<Integer> cur = vec2d.get(x);
int res = cur.get(y);
if(y == cur.size()-1){
x++;
y = 0;
while(x < vec2d.size() && (vec2d.get(x) == null || vec2d.get(x).size() == 0)){
x++;
}
} else{
y++;
}
return res;
}
@Override
public boolean hasNext() {
if(x == vec2d.size()) return false;
else return true;
}
}
/**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i = new Vector2D(vec2d);
* while (i.hasNext()) v[f()] = i.next();
*/