leetcode 341 Flatten Nested List Iterator and other Iterator collections

Keywords: Java

Iterator is actually a single linked list, and it can't be looked back. Many data structures in java have this interface, which requires initalize to get an iterator.
Calling next() returns an object, pointing to the next unvisited part.
hasnext() returns boolean, indicating whether it's at the end.

284 Peeking Iterator

class PeekingIterator implements Iterator<Integer> {
    Iterator<Integer> iter;
    Integer next = null;    // All it does is cache, because next() calls and goes to the 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 Iterator

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());
        // If you give k lists, List < List < Integer > vs, just put all v1,v1...vk iterator s into 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();
    }
}

The topic of Zigzag Iterator is similar to merge k sorted list. The idea of merge k sorted list plus OOD will be used in design twitter.

173 BST Iterator

Point here, BST inorder subtotal.
bst iterator

341 Flatten Nested List Iterator

The meaning of the title defines a special data structure, which forms many layers with parentheses and outputs them in order from left to right.
Parentheses are a strong reminder that you need to stack things in order to break them apart and keep them in their original order.
[1, [4, [6]] is stored in the stack and becomes | [4, [6], [1].|
If isInteger() is called at the top of the stack and true is returned, the getInteger() output is required.
If isInteger() is called and false is returned, getList() is used to get the same list as the original input, and the same steps are taken to store the stack.
Continuously disassemble Nested Integer and get results.
This problem is to give the API a little more, need to read the meaning of the problem, and then disassemble to get the results. The idea of stack comes from parentheses, followed by the topic of 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;
    }
}```

Posted by zggtf211 on Wed, 13 Feb 2019 01:42:19 -0800