forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
27 lines (27 loc) · 764 Bytes
/
Copy paths1.cpp
File metadata and controls
27 lines (27 loc) · 764 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
// OJ: https://leetcode.com/problems/zigzag-iterator/
// Author: github.com/lzl124631x
// Time:
// ZigzagIterator: O(K)
// next: O(1)
// hasNext: O(1)
// Space: O(K) where K is the number of input vectors
class ZigzagIterator {
private:
queue<pair<vector<int>::iterator, vector<int>::iterator>> q;
public:
ZigzagIterator(vector<int>& v1, vector<int>& v2) {
if (v1.begin() != v1.end()) q.push(make_pair(v1.begin(), v1.end()));
if (v2.begin() != v2.end()) q.push(make_pair(v2.begin(), v2.end()));
}
int next() {
auto p = q.front();
q.pop();
int val = *p.first;
p.first++;
if (p.first != p.second) q.push(p);
return val;
}
bool hasNext() {
return q.size();
}
};