leetcode 341 Flatten Nested List Iterator 以及其餘Iterator合集

Iterator其實就是一個單鏈表,沒法回頭看。java裏不少數據結構都有這個接口,使用時須要initalize,獲得一個iterator.
調用next()返回的是一個object, 指向的是下一個未訪問過的部分。
hasnext()返回的是boolean, 表示是否走到告終尾。

284 Peeking Iteratorjava

class PeekingIterator implements Iterator<Integer> {
    Iterator<Integer> iter;
    Integer next = null;    // 起到的就是緩存做用,由於next()調用以後立刻就走到下一個object了。
    
    public PeekingIterator(Iterator<Integer> iterator) {
        // initialize any member here.
        iter = iterator;
        if(iter.hasNext()){
            next = iter.next();
        }
    }

    // Returns the next element in the iteration without advancing the iterator.
    public Integer peek() {
        return next;
    }

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    @Override
    public Integer next() {
        Integer res = next;
        next = iter.hasNext() ? iter.next() : null;
        return res;
    }

    @Override
    public boolean hasNext() {
        return next != null;
    }
}

281 Zigzag Iteratorsegmentfault

public class ZigzagIterator {
    Queue<Iterator> list;
    
    public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
        list = new LinkedList<Iterator>();
        if(!v1.isEmpty()) list.add(v1.iterator());
        if(!v2.isEmpty()) list.add(v2.iterator());
        // 若是給的是k個list, List<List<Integer>> vs, 只須要把全部v1,v1...vk生成iterator放到q裏
        // for(List<Integer> v: List<List<Integer>>) if(!v.isEmpty()) list.add(v.iterator());
    }

    public int next() {
        Iterator iter = list.poll();
        int res = (Integer) iter.next();
        if(iter.hasNext()) list.offer(iter);
        return res;
    }

    public boolean hasNext() {
        return !list.isEmpty();
    }
}

Zigzag Iterator這個題目和merge k sorted list很像,design twitter用到的就是merge k sorted list的思想加上OOD,會另寫一篇。緩存

173 BST Iterator數據結構

戳這裏,BST inorder小專題。
bst iteratoride

341 Flatten Nested List Iteratorthis

題目的意思定義了一個特殊的數據結構,用括號造成不少層,按從左到右的順序輸出。
括號是個強提示,要把括號裏的東西拆開還保持原來的順序,就須要用stack處理。
[1,[4,[6]]] 存到stack裏,變成|[4,[6]], [1]|
棧頂調用isInteger(),返回true, 就要getInteger()輸出。
若是調用isInteger(),返回false, 用getList()獲得和最初的輸入相同的list,作相同的步驟存入stack.
不斷拆開NestedInteger, 獲得結果。
這題就是給的API有點多,須要讀懂題意,而後再拆開獲得結果。stack思想就是來自括號,後面也會跟進stack的專題.
/**
 * // 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> {
    ArrayDeque<NestedInteger> stk;
    
    public NestedIterator(List<NestedInteger> nestedList) {
        stk = new ArrayDeque<NestedInteger>();
        for(int i = nestedList.size()-1; i >= 0; i--){
            stk.addLast(nestedList.get(i));
        }
    }

    @Override
    public Integer next() {
        return stk.pollLast().getInteger();
    }

    @Override
    public boolean hasNext() {
        while(!stk.isEmpty()){
            NestedInteger cur = stk.peekLast();
            if(cur.isInteger()){
                return true;
            }
            List<NestedInteger> list = stk.pollLast().getList();
            for(int i=list.size()-1; i>=0; i--){
                stk.addLast(list.get(i));
            }
        }
        return false;
    }
}```
相關文章
相關標籤/搜索