-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode251.java
42 lines (38 loc) · 1006 Bytes
/
leetcode251.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> {
Queue<Iterator<Integer>> queue;
Iterator<Integer> current;
public Vector2D(List<List<Integer>> vec2d) {
queue = new LinkedList<Iterator<Integer>>();
for(List<Integer> list : vec2d){
if(!list.isEmpty()){
queue.add(list.iterator());
}
}
current = queue.poll();
}
@Override
public Integer next() {
if(!current.hasNext())
return -1;
return current.next();
}
@Override
public boolean hasNext() {
if(current == null)
return false;
while(!current.hasNext()){
if(!queue.isEmpty()){
current = queue.poll();
}
else{
return false;
}
}
return true;
}
}
/**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i = new Vector2D(vec2d);
* while (i.hasNext()) v[f()] = i.next();
*/