forked from deepaktalwardt/interview-prep-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path281-zigzag-iterator.cpp
46 lines (43 loc) · 1.05 KB
/
281-zigzag-iterator.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
class ZigzagIterator {
public:
ZigzagIterator(vector<int>& v1, vector<int>& v2) : v1(v1), v2(v2) {
if (v1.size() != 0) {
nextIsV1 = true;
} else {
nextIsV1 = false;
}
idx = 0;
}
int next() {
int toReturn = nextIsV1 ? v1[idx] : v2[idx];
if (nextIsV1) {
if (idx < v2.size()) {
nextIsV1 = false;
} else {
idx++;
}
} else {
if (++idx < v1.size()) {
nextIsV1 = true;
}
}
return toReturn;
}
bool hasNext() {
if (nextIsV1 && idx < v1.size()) {
return true;
} else if (!nextIsV1 && idx < v2.size()) {
return true;
}
return false;
}
private:
bool nextIsV1;
int idx;
vector<int> &v1, &v2;
};
/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i(v1, v2);
* while (i.hasNext()) cout << i.next();
*/