Wednesday, July 29, 2020

341. Flatten Nested List Iterator -M !!!!!!!!!!!!!!! 方法2,简洁多了

Given a nested list of integers, implement an iterator to flatten it.
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
Example 1:
Input: [[1,1],2,[1,1]]
Output: [1,1,2,1,1]
Explanation: By calling next repeatedly until hasNext returns false, 
             the order of elements returned by next should be: [1,1,2,1,1].
Example 2:
Input: [1,[4,[6]]]
Output: [1,4,6]
Explanation: By calling next repeatedly until hasNext returns false, 
             the order of elements returned by next should be: [1,4,6].

A:

在stack中保持 List
/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * class NestedInteger {
 *   public:
 *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     bool isInteger() const;
 *
 *     // Return the single integer that this NestedInteger holds, if it holds a single integer
 *     // The result is undefined if this NestedInteger holds a nested list
 *     int getInteger() const;
 *
 *     // Return the nested list that this NestedInteger holds, if it holds a nested list
 *     // The result is undefined if this NestedInteger holds a single integer
 *     const vector<NestedInteger> &getList() const;
 * };
 */

class NestedIterator {
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        sList.push_back(nestedList);
        sIndex.push_back(-1);
    }
    
    int next() {
        return sList.back()[sIndex.back()].getInteger();
    }
    
    bool hasNext() {
        while(!sList.empty()){
            int i = sIndex.back() + 1;
            sIndex.pop_back();
            if(i>= sList.back().size()){
                sList.pop_back(); // discard the current run-out list
                if(!sIndex.empty()){// if not empty, increase last index by 1
                    int tmp = sIndex.back();
                    sIndex.pop_back();
                    sIndex.push_back(tmp+1);
                }
            }else{
                auto item = sList.back();
                if(item[i].isInteger()){
                    sIndex.push_back(i);
                    return true;
                }else{
                    sIndex.push_back(i-1);
                    sIndex.push_back(-1);
                    sList.push_back(item[i].getList());
                }
            }
        }
        return false;
    }
private:
    vector<vector<NestedInteger>> sList;
    vector<int> sIndex;
};

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i(nestedList);
 * while (i.hasNext()) cout << i.next();
 */

******************************************************************
在stack中,直接保持了NestedInteger

class NestedIterator {
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        for(auto it = nestedList.rbegin(); it != nestedList.rend(); ++it)
            S.push(*it);
    }
    
    int next() {
        int res = S.top().getInteger();
        S.pop();
        return res;
    }
    
    bool hasNext() {
        while(!S.empty() && !S.top().isInteger()){
            auto list = S.top().getList();
            S.pop();
            for(auto it = list.rbegin(); it != list.rend(); ++it)
                S.push(*it);            
        }
        return !S.empty();
    }
private:
    stack<NestedInteger> S;
};



No comments:

Post a Comment