1. Given a nested list of integers, implement an iterator to flatten it.
  2.  
  3. Each element is either an integer, or a list -- whose elements may also be integers or other lists.
  4.  
  5. Example 1:
  6. Given the list [[1,1],2,[1,1]],
  7.  
  8. By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
  9.  
  10. Example 2:
  11. Given the list [1,[4,[6]]],
  12.  
  13. 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入栈

  1. /**
  2. * // This is the interface that allows for creating nested lists.
  3. * // You should not implement it, or speculate about its implementation
  4. * public interface NestedInteger {
  5. *
  6. * // @return true if this NestedInteger holds a single integer, rather than a nested list.
  7. * public boolean isInteger();
  8. *
  9. * // @return the single integer that this NestedInteger holds, if it holds a single integer
  10. * // Return null if this NestedInteger holds a nested list
  11. * public Integer getInteger();
  12. *
  13. * // @return the nested list that this NestedInteger holds, if it holds a nested list
  14. * // Return null if this NestedInteger holds a single integer
  15. * public List<NestedInteger> getList();
  16. * }
  17. */
  18. public class NestedIterator implements Iterator<Integer> {
  19. Stack<NestedInteger> st;
  20.  
  21. public NestedIterator(List<NestedInteger> nestedList) {
  22. st = new Stack<NestedInteger>();
  23. for (int i=nestedList.size()-1; i>=0; i--) {
  24. st.push(nestedList.get(i));
  25. }
  26. }
  27.  
  28. @Override
  29. public Integer next() {
  30. return st.pop().getInteger();
  31. }
  32.  
  33. @Override
  34. public boolean hasNext() {
  35. while (!st.isEmpty()) {
  36. if (st.peek().isInteger()) return true;
  37. List<NestedInteger> cur = st.pop().getList();
  38. for (int i=cur.size()-1; i>=0; i--) {
  39. st.push(cur.get(i));
  40. }
  41. }
  42. return false;
  43. }
  44. }
  45.  
  46. /**
  47. * Your NestedIterator object will be instantiated and called as such:
  48. * NestedIterator i = new NestedIterator(nestedList);
  49. * while (i.hasNext()) v[f()] = i.next();
  50. */

Leetcode: Flatten Nested List Iterator的更多相关文章

  1. [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  2. [LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  3. [LeetCode] 341. Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  4. 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...

  5. [leetcode]341. Flatten Nested List Iterator展开嵌套列表的迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  6. 【leetcode】341. Flatten Nested List Iterator

    题目如下: Given a nested list of integers, implement an iterator to flatten it. Each element is either a ...

  7. LeetCode 341. Flatten Nested List Iterator

    https://leetcode.com/problems/flatten-nested-list-iterator/

  8. [Swift]LeetCode341. 压平嵌套链表迭代器 | Flatten Nested List Iterator

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  9. Flatten Nested List Iterator

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

随机推荐

  1. __LINE__ check_arr_empty($arr)

    <?php $arr = array('','',''); foreach($arr as $w) { // w(empty($w)); } w(empty($arr)); w(check_ar ...

  2. base64coder调用

    base64coder 可以查看官网:   http://www.source-code.biz/base64coder/java/ 我所涉及到的  base64coder调用: 某天,因需要修改Pr ...

  3. PDO知识

    PDO: 一.含义: 数据访问抽象层 二.作用 :通过PDO能够访问其它的数据库 三. 用法: 1.造对象 ①$dsn="mysql:dbname=zz;host=localhost&quo ...

  4. BLE 4.0 与 4.1的区别

    蓝牙技术让我们在连接各种设备的时候不再被繁多的数据线所束缚,比如音响.电脑,甚至是汽车.目前最新的蓝牙版本是4.0,相比3.0它进一步降低了功耗,并且也提高了传输效率.近日,蓝牙技术联盟(Blueto ...

  5. 查询mysql当前连接数

    标签: mysql服务器cachedisk 2012-08-23 23:06 23377人阅读 评论(0) 收藏 举报  分类: MySql(36)  1.show status Threads_co ...

  6. git中应用在vs中使用gitignore (转)

    在进行协作开发代码管理的过程中,常常会遇到某些临时文件.配置文件.或者生成文件等,这些文件由于不同的开发端会不一样,如果使用git add . 将所有文件纳入git库中,那么会出现频繁的改动和push ...

  7. SQL集合函数中利用case when then 技巧

    我们都知道SQL中适用case when then来转化数据库中的信息 比如  select (case sex when 0 then '男' else '女' end) AS sex  from ...

  8. node express 学习2

    上次我们的express已经安装好了 接下来我们修改渲染引擎为html // view engine setup app.set('views', path.join(__dirname, 'view ...

  9. 简述C#中关键字var和dynamic的区别

    C#中关键字var和dynamic的区别如下: 1.var申明的变量必须初始化,dynamic申明的变量无需初始化. 2.var关键字只能在方法内部申明局部变量,dynamic关键字可用于局部变量,字 ...

  10. css 清除浮动(转)

    转自http://hi.baidu.com/kongcheng2012/item/2b1250d4452e802538f6f705 为什么浮动这么难? 因为浮动会使当前标签产生向上浮的效果,同时会影响 ...