C++ accepter solution using queue


  • 0
    V
    class NestedIterator {
    
    public:
        NestedIterator(vector<NestedInteger> &nestedList) {
            for( const auto& i : nestedList)
                pushIntToQueue(i);
        }
    
        int next() {
            int val = intQueue.front();
            intQueue.pop();
            return val;
        }
    
        bool hasNext() {
            return !intQueue.empty();
        }
    
        void pushIntToQueue(const NestedInteger& nestedInteger)
        {
            if(nestedInteger.isInteger())
                intQueue.push(nestedInteger.getInteger());
            else
                for(const auto& i: nestedInteger.getList())
                    pushIntToQueue(i);
        }
        queue<int> intQueue;
    };

Log in to reply
 

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