Share my c++ sulotion


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

Log in to reply
 

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