forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
54 lines (50 loc) · 919 Bytes
/
main.cpp
File metadata and controls
54 lines (50 loc) · 919 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class ZigzagIterator {
int current_it;
int it1;
int it2;
vector<int> vec1;
vector<int> vec2;
public:
ZigzagIterator(vector<int>& v1, vector<int>& v2) {
current_it = 1;
it1 = 0;
it2 = 0;
vec1 = v1;
vec2 = v2;
}
int next() {
if (current_it == 1) {
current_it = 2;
if (it1 < vec1.size()) {
it1++;
return vec1[it1 - 1];
}
}
// else
current_it = 1;
it2++;
return vec2[it2 - 1];
}
bool hasNext() {
return it1 < vec1.size() || it2 < vec2.size();
}
};
int main() {
vector<int> v1 = {1, 2};
vector<int> v2 = {3, 4, 5, 6};
ZigzagIterator i(v1, v2);
while (i.hasNext()) {
cout << i.next();
}
return 0;
}