Stack


  • 0
    N
    class NestedIterator {
        stack<NestedInteger> stk;
    public:
        NestedIterator(vector<NestedInteger> &nestedList) {
            for (int i = nestedList.size() - 1; i >= 0; i--) {
                stk.push(nestedList[i]);
            }
        }
    
        int next() {
            int ret = stk.top().getInteger();
            stk.pop();
            return ret;
        }
    
        bool hasNext() {
            vector<NestedInteger> list;
            
            while (!stk.empty() &&!stk.top().isInteger()) {
                list = stk.top().getList();
                stk.pop();
                for (int i = list.size() - 1; i >= 0; i--) {
                    stk.push(list[i]);
                }
            }
            
            return stk.size();
        }
    };
    

    Takeaway: [[]] situation. I should have notice this when list.size()...


Log in to reply
 

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