-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path281.cpp
More file actions
28 lines (25 loc) · 701 Bytes
/
281.cpp
File metadata and controls
28 lines (25 loc) · 701 Bytes
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
class ZigzagIterator {
public:
vector<vector<int>*> v;
queue<pair<int, int>> q;
ZigzagIterator(vector<int>& v1, vector<int>& v2) {
if (v1.size() > 0) q.push(make_pair(0, 0));
if (v2.size() > 0) q.push(make_pair(1, 0));
v.push_back(&v1);
v.push_back(&v2);
}
int next() {
auto [vIdx, loc] = q.front();
q.pop();
if (loc + 1 != v[vIdx]->size()) q.push(make_pair(vIdx, loc + 1));
return (*v[vIdx])[loc];
}
bool hasNext() {
return (q.size() > 0);
}
};
/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i(v1, v2);
* while (i.hasNext()) cout << i.next();
*/