AC C++ solution need to simplify somehow...


  • 0
    class NestedIterator {
        vector<int> flat;
        int Index;
        void flatInt(vector<NestedInteger> &nestedList)
        {
            for(int i=0; i<nestedList.size(); i++)
            {
                if(nestedList[i].isInteger()) 
                    flat.push_back(nestedList[i].getInteger());
                else
                    flatInt(nestedList[i].getList());
            }
        }
    public:
        NestedIterator(vector<NestedInteger> &nestedList) {
            Index = -1;
            for(int i=0; i<nestedList.size(); i++)
            {
                if(nestedList[i].isInteger()) 
                    flat.push_back(nestedList[i].getInteger());
                else
                    flatInt(nestedList[i].getList());
            }
        }
    
        int next() {
            return flat[++Index];
        }
    
        bool hasNext() {
            return flat.size()>Index+1;
        }
    };
    

Log in to reply
 

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