Leetcode: 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]],
- 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].
非常精巧地使用stack。push all the nestedList into the stack from back to front,so when we pop the stack, it returns the very first element
执行hasNext()的时候,如果peek()是integer, return true;否则是一个List<NestedInteger>, 则flatten这个list,同样的,把list里的NestedInteger倒序push入栈
- /**
- * // 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> st;
- public NestedIterator(List<NestedInteger> nestedList) {
- st = new Stack<NestedInteger>();
- for (int i=nestedList.size()-1; i>=0; i--) {
- st.push(nestedList.get(i));
- }
- }
- @Override
- public Integer next() {
- return st.pop().getInteger();
- }
- @Override
- public boolean hasNext() {
- while (!st.isEmpty()) {
- if (st.peek().isInteger()) return true;
- List<NestedInteger> cur = st.pop().getList();
- for (int i=cur.size()-1; i>=0; i--) {
- st.push(cur.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: Flatten Nested List Iterator的更多相关文章
- [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [LeetCode] 341. Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- [leetcode]341. Flatten Nested List Iterator展开嵌套列表的迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- 【leetcode】341. Flatten Nested List Iterator
题目如下: Given a nested list of integers, implement an iterator to flatten it. Each element is either a ...
- LeetCode 341. Flatten Nested List Iterator
https://leetcode.com/problems/flatten-nested-list-iterator/
- [Swift]LeetCode341. 压平嵌套链表迭代器 | Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
随机推荐
- __LINE__ check_arr_empty($arr)
<?php $arr = array('','',''); foreach($arr as $w) { // w(empty($w)); } w(empty($arr)); w(check_ar ...
- base64coder调用
base64coder 可以查看官网: http://www.source-code.biz/base64coder/java/ 我所涉及到的 base64coder调用: 某天,因需要修改Pr ...
- PDO知识
PDO: 一.含义: 数据访问抽象层 二.作用 :通过PDO能够访问其它的数据库 三. 用法: 1.造对象 ①$dsn="mysql:dbname=zz;host=localhost&quo ...
- BLE 4.0 与 4.1的区别
蓝牙技术让我们在连接各种设备的时候不再被繁多的数据线所束缚,比如音响.电脑,甚至是汽车.目前最新的蓝牙版本是4.0,相比3.0它进一步降低了功耗,并且也提高了传输效率.近日,蓝牙技术联盟(Blueto ...
- 查询mysql当前连接数
标签: mysql服务器cachedisk 2012-08-23 23:06 23377人阅读 评论(0) 收藏 举报 分类: MySql(36) 1.show status Threads_co ...
- git中应用在vs中使用gitignore (转)
在进行协作开发代码管理的过程中,常常会遇到某些临时文件.配置文件.或者生成文件等,这些文件由于不同的开发端会不一样,如果使用git add . 将所有文件纳入git库中,那么会出现频繁的改动和push ...
- SQL集合函数中利用case when then 技巧
我们都知道SQL中适用case when then来转化数据库中的信息 比如 select (case sex when 0 then '男' else '女' end) AS sex from ...
- node express 学习2
上次我们的express已经安装好了 接下来我们修改渲染引擎为html // view engine setup app.set('views', path.join(__dirname, 'view ...
- 简述C#中关键字var和dynamic的区别
C#中关键字var和dynamic的区别如下: 1.var申明的变量必须初始化,dynamic申明的变量无需初始化. 2.var关键字只能在方法内部申明局部变量,dynamic关键字可用于局部变量,字 ...
- css 清除浮动(转)
转自http://hi.baidu.com/kongcheng2012/item/2b1250d4452e802538f6f705 为什么浮动这么难? 因为浮动会使当前标签产生向上浮的效果,同时会影响 ...