Short C++ 16ms using deque


  • 0
    class NestedIterator {
    private:
        vector<NestedInteger>nestedList; 
        deque<int>current;
        int k=0;
        
    public:
        NestedIterator(vector<NestedInteger> &nestedList) {
            this->nestedList = nestedList;
        }
    
        int next() {
            int num = current.front();
            current.pop_front();
            return num;
        }
    
        bool hasNext() {
            while(k != nestedList.size() && current.empty()){
                if(nestedList[k].isInteger()) {
                    current.push_back(nestedList[k].getInteger());
                    k++;
                    break;
                }
                NestedIterator i(nestedList[k].getList());
                while(i.hasNext()) current.push_back(i.next());
                k++;
            }
            return !current.empty();
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.