[LeetCode] Flatten Nested List Iterator 壓平嵌套鏈表迭代器


 

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:
Given the list [[1,1],2,[1,1]],

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].

Example 2:
Given the list [1,[4,[6]]],

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].

 

這道題讓我們建立壓平嵌套鏈表的迭代器,關於嵌套鏈表的數據結構最早出現在Nested List Weight Sum中,而那道題是用的遞歸的方法來解的,而迭代器一般都是用迭代的方法來解的,而遞歸一般都需用棧來輔助遍歷,由於棧的后進先出的特性,我們在對向量遍歷的時候,從后往前把對象壓入棧中,那么第一個對象最后壓入棧就會第一個取出來處理,我們的hasNext()函數需要遍歷棧,並進行處理,如果棧頂元素是整數,直接返回true,如果不是,那么移除棧頂元素,並開始遍歷這個取出的list,還是從后往前壓入棧,循環停止條件是棧為空,返回false,參見代碼如下:

 

解法一:

class NestedIterator {
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        for (int i = nestedList.size() - 1; i >= 0; --i) {
            s.push(nestedList[i]);
        }
    }

    int next() {
        NestedInteger t = s.top(); s.pop();
        return t.getInteger();
    }

    bool hasNext() {
        while (!s.empty()) {
            NestedInteger t = s.top(); 
            if (t.isInteger()) return true;
            s.pop();
            for (int i = t.getList().size() - 1; i >= 0; --i) {
                s.push(t.getList()[i]);
            }
        }
        return false;
    }
private: stack<NestedInteger> s; };

 

我們也可以使用deque來代替stack,實現思路和上面完全一樣,參見代碼如下:

 

解法二:

class NestedIterator {
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        for (auto a : nestedList) {
            d.push_back(a);
        }
    }

    int next() {
        NestedInteger t = d.front(); d.pop_front();
        return t.getInteger();
    }

    bool hasNext() {
        while (!d.empty()) {
            NestedInteger t = d.front();
            if (t.isInteger()) return true;
            d.pop_front();
            for (int i = 0; i < t.getList().size(); ++i) {
                d.insert(d.begin() + i, t.getList()[i]);
            }
        }
        return false;
    }

private:
    deque<NestedInteger> d;
};

 

雖說迭代器是要用迭代的方法,但是我們可以強行使用遞歸來解,怎么個強行法呢,就是我們使用一個隊列queue,在構造函數的時候就利用迭代的方法把這個嵌套鏈表全部壓平展開,然后在調用hasNext()和next()就很簡單了:

 

解法三:

class NestedIterator {
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        make_queue(nestedList);
    }

    int next() {
        int t = q.front(); q.pop();
        return t; 
    }

    bool hasNext() {
        return !q.empty();
    }
    
private:
    queue<int> q;
    void make_queue(vector<NestedInteger> &nestedList) {
        for (auto a : nestedList) {
            if (a.isInteger()) q.push(a.getInteger());
            else make_queue(a.getList());
        }
    }
};

 

類似題目:

Nested List Weight Sum

Flatten 2D Vector

Zigzag Iterator

 

參考資料:

https://leetcode.com/discuss/95841/simple-solution-with-queue

https://leetcode.com/discuss/95892/concise-c-without-storing-all-values-at-initialization

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM