class ZigzagIterator {
public:
ZigzagIterator(vector<int>& v1, vector<int>& v2) {
}
int next() {
}
bool hasNext() {
}
};
/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i(v1, v2);
* while (i.hasNext()) cout << i.next();
*/
class ZigzagIterator:
def __init__(self, v1: List[int], v2: List[int]):
p = 0
self.arr = []
while p < len(v1) and p < len(v2):
self.arr.append(v1[p])
self.arr.append(v2[p])
p += 1
self.arr += v1[p:]
self.arr += v2[p:]
self.p = 0
def next(self) -> int:
self.p += 1
return self.arr[self.p - 1]
def hasNext(self) -> bool:
return self.p < len(self.arr)
# Your ZigzagIterator object will be instantiated and called as such:
# i, v = ZigzagIterator(v1, v2), []
# while i.hasNext(): v.append(i.next())
public class ZigzagIterator {
Queue<Iterator<Integer>> queue = new LinkedList<>();
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
Iterator<Integer> it1 = v1.iterator();
Iterator<Integer> it2 = v2.iterator();
if (it1.hasNext()) {
queue.add(it1);
}
if (it2.hasNext()) {
queue.add(it2);
}
}
public int next() {
Iterator<Integer> it = queue.poll();
int v = it.next();
if (it.hasNext()) {
queue.add(it);
}
return v;
}
public boolean hasNext() {
if (!queue.isEmpty()) {
return true;
}
return false;
}
}
/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i = new ZigzagIterator(v1, v2);
* while (i.hasNext()) v[f()] = i.next();
*/