-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cpp
47 lines (40 loc) · 993 Bytes
/
Solution.cpp
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
43
44
45
46
47
#include <cstddef>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
/**
* A queue can be used
*/
class ZigzagIterator {
private:
// {iter, end}
queue<pair<vector<int>::iterator, vector<int>::iterator>> iters;
public:
ZigzagIterator(vector<int>& v1, vector<int>& v2) {
// Only need one check. Since a call to next() will be valid.
if (!v1.empty()) {
iters.emplace(v1.begin(), v1.end());
}
if (!v2.empty()) {
iters.emplace(v2.begin(), v2.end());
}
}
int next() {
auto [iterBegin, iterEnd] = iters.front();
iters.pop();
// Guaranteed valid
const int val = *iterBegin;
iterBegin = std::next(iterBegin);
if (iterBegin != iterEnd) {
iters.emplace(iterBegin, iterEnd);
}
return val;
}
bool hasNext() { return !iters.empty(); }
};
/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i(v1, v2);
* while (i.hasNext()) cout << i.next();
*/