leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0341.cpp (699B)
0 class NestedIterator {
1 stack<pair<vector<NestedInteger> *, int>> st;
3 public:
4 NestedIterator(vector<NestedInteger> &nestedList) { st.push({&nestedList, 0}); }
5 int next() {
6 if (!hasNext()) return -1;
7 auto &elem = st.top();
8 return (*elem.first)[elem.second++].getInteger();
9 }
11 bool hasNext() {
12 while (!st.empty()) {
13 auto &elem = st.top();
14 if (elem.second == elem.first->size()) {
15 st.pop();
16 continue;
17 }
18 if ((*elem.first)[elem.second].isInteger()) return true;
19 st.push({&(*elem.first)[elem.second++].getList(), 0});
20 }
21 return false;
22 }
23 };