341. 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]],ide

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]]],this

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

[暴力解法]:debug

時間分析:code

空間分析:blog

 [優化後]:

時間分析:

空間分析:

[奇葩輸出條件]:

[奇葩corner case]:

[思惟問題]:

不知道.next 和 .hasnext有啥區別:取出來、只是看看有沒有

[一句話思路]:

只有stack才能一次取出來一層,getlist getinteger

[輸入量]:空: 正常狀況:特大:特小:程序裏處理到的特殊狀況:異常狀況(不合法不合理的輸入):

[畫圖]:

[一刷]:

  1. .next創建在.hasnext的基礎之上,因此hasnext須要放在while循環中,作完爲止
  2. curr若是是個list,就必須用專有方法
    curr.getList()
    先取出,再作後續操做

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分鐘肉眼debug的結果]:

[總結]:

一次放一層

[複雜度]:Time complexity: O(n) Space complexity: O(n)

[英文數據結構或算法,爲何不用別的數據結構或算法]:

list 對應的方法是.size() .get

只有stack才能一次取出來一層,數組不能直接取出來一層。因此用stack。

stack有

.getInteger()
.getList()

方法

[算法思想:遞歸/分治/貪心]:

[關鍵模板化代碼]:

public NestedIterator(List<NestedInteger> nestedList) {
    //put into stack from back
        for (int i = nestedList.size() - 1; i >= 0; i--) stack.push(nestedList.get(i));
    }

[其餘解法]:

[Follow Up]:

[LC給出的題目變變變]:

 [代碼風格] :

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * public interface NestedInteger {
 *
 *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
 *     public boolean isInteger();
 *
 *     // @return the single integer that this NestedInteger holds, if it holds a single integer
 *     // Return null if this NestedInteger holds a nested list
 *     public Integer getInteger();
 *
 *     // @return the nested list that this NestedInteger holds, if it holds a nested list
 *     // Return null if this NestedInteger holds a single integer
 *     public List<NestedInteger> getList();
 * }
 */
public class NestedIterator implements Iterator<Integer> {
    //ini:stack
    Stack<NestedInteger> stack = new Stack<>();
    
    public NestedIterator(List<NestedInteger> nestedList) {
    //put into stack from back
        for (int i = nestedList.size() - 1; i >= 0; i--) stack.push(nestedList.get(i));
    }

    @Override
    public Integer next() {
    //pop
        return stack.pop().getInteger();
    }

    @Override
    public boolean hasNext() {
        while (!stack.isEmpty()) {
        //isInteger or put into stack from back
        NestedInteger curr = stack.peek();
        if (curr.isInteger()) return true;
        
        stack.pop();
        for (int i = curr.getList().size() - 1; i >= 0; i--) {
            stack.push(curr.getList().get(i));
        }
        
        
        }
        return false;
    }
}

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i = new NestedIterator(nestedList);
 * while (i.hasNext()) v[f()] = i.next();
 */
View Code
相關文章
相關標籤/搜索