Leetcode 341. Flatten Nested List Iterator
来源:互联网 发布:淘宝如何清空收藏夹 编辑:程序博客网 时间:2024/06/08 13:59
341. Flatten Nested List Iterator
Total Accepted: 17597 Total Submissions: 49172 Difficulty: MediumGiven 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]
.
思路:
自己的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(); */
- leetcode 341. Flatten Nested List Iterator
- [leetcode] 341. Flatten Nested List Iterator
- LeetCode 341. Flatten Nested List Iterator
- LeetCode 341. Flatten Nested List Iterator
- [leetcode] 341. Flatten Nested List Iterator
- LeetCode: 341. Flatten Nested List Iterator
- Leetcode 341. Flatten Nested List Iterator
- 【LeetCode】 341. Flatten Nested List Iterator
- leetcode 341. Flatten Nested List Iterator
- LeetCode 341. Flatten Nested List Iterator
- [leetcode]341. Flatten Nested List Iterator
- leetcode 341. Flatten Nested List Iterator
- leetcode 341. Flatten Nested List Iterator
- leetcode 341. Flatten Nested List Iterator
- Leetcode 341. Flatten Nested List Iterator
- LeetCode|Flatten Nested List Iterator
- [LeetCode] Flatten Nested List Iterator
- LeetCode:Flatten Nested List Iterator
- DIV与前面的控件在同一行显示.
- 手把手教你如何安装和使用Karma-Jasmine
- 剑指Offer 25题 二叉树中和为某一值的路径 Java
- C++获取当前路径
- 常用的项目管理工具JIRA | ...
- Leetcode 341. Flatten Nested List Iterator
- Python.微信
- C++创建文件夹
- 关于Windows用Task Scheduling执行C++ exe文件
- 10.Doctrine2 (2)
- Leetcode 215. Kth Largest Element in an Array
- [LeetCode]100. Same Tree
- spring aop(面向切面) 详解
- vec