Skip to content

Latest commit

 

History

History
29 lines (24 loc) · 536 Bytes

File metadata and controls

29 lines (24 loc) · 536 Bytes
  • 不断递归直到获取单元素即可
class NestedIterator {
 public:
  NestedIterator(vector<NestedInteger>& nestedList) { append(nestedList, q); }

  void append(vector<NestedInteger>& nestedList, queue<int>& q) {
    for (auto& x : nestedList) {
      if (x.isInteger()) {
        q.emplace(x.getInteger());
      } else {
        append(x.getList(), q);
      }
    }
  }

  int next() {
    int res = q.front();
    q.pop();
    return res;
  }

  bool hasNext() { return !empty(q); }

 private:
  queue<int> q;
};