-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigzag-iterator.cpp
More file actions
28 lines (25 loc) · 829 Bytes
/
zigzag-iterator.cpp
File metadata and controls
28 lines (25 loc) · 829 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:
// ????https://leetcode.com/problems/zigzag-iterator/discuss/71835/C++-with-queue-(compatible-with-k-vectors)
// ?????????https://leetcode.com/submissions/detail/264979672/
queue<pair<vector<int>::iterator,vector<int>::iterator>> q;
ZigzagIterator(vector<int>& v1, vector<int>& v2) {
if(!v1.empty()) q.push({v1.begin(),v1.end()});
if(!v2.empty()) q.push({v2.begin(),v2.end()});
}
int next() {
auto it=q.front().first;
auto end=q.front().second;
q.pop();
if(it+1!=end) q.push({it+1,end});
return *it;
}
bool hasNext() {
return !q.empty();
}
};
/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i(v1, v2);
* while (i.hasNext()) cout << i.next();
*/