Leetcode 341. Flatten Nested List Iterator

来源:互联网 发布:淘宝如何清空收藏夹 编辑:程序博客网 时间:2024/06/08 13:59

341. Flatten Nested List Iterator

Total Accepted: 17597 Total Submissions: 49172 Difficulty: Medium

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].

Hide Company Tags
 Google Facebook Twitter
Hide Tags
 Stack Design
Hide Similar Problems
 (M) Flatten 2D Vector (M) Zigzag Iterator (M) Mini Parser

思路:

自己的implementation没有用stack。用个LinkedList保存所有的数据,在初始化Iterator的时候就存好所有的数据。


7ms:

/** * // 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> {    LinkedList<Integer> buff = new LinkedList<Integer>();        public NestedIterator(List<NestedInteger> nestedList) {        helper(nestedList);    }    @Override    public Integer next() {        if(hasNext()) return buff.removeFirst();        return -1;    }    @Override    public boolean hasNext() {        return buff.size() == 0 ? false : true;    }        public void helper(List<NestedInteger> nestedList){        for(NestedInteger item : nestedList){            if(item.isInteger()) buff.add(item.getInteger());            else helper(item.getList());        }    }}/** * Your NestedIterator object will be instantiated and called as such: * NestedIterator i = new NestedIterator(nestedList); * while (i.hasNext()) v[f()] = i.next(); */

Stack版本:

从后向前压栈,然后next就是去除栈顶元素输出。因为next一定在hasNext之后调用。hasNext每次检查栈顶元素是不是Integer,是返回true;否则取出其List从后往前压栈,重复这个过程直到第一个元素是Integer。

运行时间:12ms。代码来自这里。

/** * // 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> {    Stack<NestedInteger> stack = new Stack<NestedInteger>();     public NestedIterator(List<NestedInteger> nestedList) {        if(nestedList==null)            return;         for(int i=nestedList.size()-1; i>=0; i--){            stack.push(nestedList.get(i));        }    }     @Override    public Integer next() {        return stack.pop().getInteger();    }     @Override    public boolean hasNext() {        while(!stack.isEmpty()){            NestedInteger top = stack.peek();            if(top.isInteger()){                return true;            }else{                stack.pop();                for(int i=top.getList().size()-1; i>=0; i--){                    stack.push(top.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(); */


0 0
原创粉丝点击