-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0341.cpp
More file actions
24 lines (22 loc) · 699 Bytes
/
Copy path0341.cpp
File metadata and controls
24 lines (22 loc) · 699 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
class NestedIterator {
stack<pair<vector<NestedInteger> *, int>> st;
public:
NestedIterator(vector<NestedInteger> &nestedList) { st.push({&nestedList, 0}); }
int next() {
if (!hasNext()) return -1;
auto &elem = st.top();
return (*elem.first)[elem.second++].getInteger();
}
bool hasNext() {
while (!st.empty()) {
auto &elem = st.top();
if (elem.second == elem.first->size()) {
st.pop();
continue;
}
if ((*elem.first)[elem.second].isInteger()) return true;
st.push({&(*elem.first)[elem.second++].getList(), 0});
}
return false;
}
};