C++ simple solution using queue


  • 0
    J

    '''

    queue<int> q;
    void dfs(queue<int> &q, vector<NestedInteger> &cur)
    {
        for (int i = 0; i < cur.size(); i++)
        {
            if (cur[i].isInteger())
            {
                q.push(cur[i].getInteger());
            }
            else
            {
                dfs(q, cur[i].getList());
            }
        }
    }
    
    NestedIterator(vector<NestedInteger> &nestedList) {
        dfs(q, nestedList);
    }
    
    int next() {
        if (hasNext()) {
            int ret = q.front();
            q.pop();
            return ret;
        }
        return -1;
    }
    
    bool hasNext() {
        return !q.empty();
    }
    

    '''


Log in to reply
 

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