leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0341.cpp (699B)


      1 class NestedIterator {
      2     stack<pair<vector<NestedInteger> *, int>> st;
      3 
      4   public:
      5     NestedIterator(vector<NestedInteger> &nestedList) { st.push({&nestedList, 0}); }
      6     int next() {
      7         if (!hasNext()) return -1;
      8         auto &elem = st.top();
      9         return (*elem.first)[elem.second++].getInteger();
     10     }
     11 
     12     bool hasNext() {
     13         while (!st.empty()) {
     14             auto &elem = st.top();
     15             if (elem.second == elem.first->size()) {
     16                 st.pop();
     17                 continue;
     18             }
     19             if ((*elem.first)[elem.second].isInteger()) return true;
     20             st.push({&(*elem.first)[elem.second++].getList(), 0});
     21         }
     22         return false;
     23     }
     24 };