题目

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

这标题,翻译的是人话吗?啥叫扁平化嵌套列表迭代器?。。

题解

比较像深度优先搜索。思路是:先 flatten 成 list 存好,需要的时候直接返回。感觉没有 get 到这个问题的精髓。评论区也有人质疑这种方法:

关于这个问题,我一开始也没打算先存成 list,仅存储调用的指针,没写出来,就先用了个 list 存数据了。

/*** // 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 empty list if this NestedInteger holds a single integer*     public List<NestedInteger> getList();* }*/
public class NestedIterator implements Iterator<Integer> {List<Integer> list;int index;public NestedIterator(List<NestedInteger> nestedList) {list = new ArrayList<>();flatten(list, nestedList);index = 0;}// Lying flat is standing up, horizontally. Lying flat is having a backbone.public void flatten(List<Integer> list, List<NestedInteger> nestedList) {for (NestedInteger next : nestedList) {if (next.isInteger()) {list.add(next.getInteger());} else {flatten(list, next.getList());}}}@Overridepublic Integer next() {if (index < list.size()) return list.get(index++);else return null;}@Overridepublic boolean hasNext() {return index < list.size();}
}/*** 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 | 341. 扁平化嵌套列表迭代器(Java)相关推荐

  1. 341. 扁平化嵌套列表迭代器

    2020-05-24 1.题目描述 扁平化嵌套列表迭代器 2.题解 使用递归即可 3.代码 /*** // This is the interface that allows for creating ...

  2. 力扣--扁平化嵌套列表迭代器

    扁平化嵌套列表迭代器 文章目录 扁平化嵌套列表迭代器 一.题目描述 二.分析 方法一: 代码一: 方法二: 代码二: C++代码: 一.题目描述 /*** // This is the interfa ...

  3. 扁平化嵌套列表迭代器 [树的递归前序遍历 + 迭代前序遍历]

    递归|迭代的前序遍历 前言 一.扁平化嵌套列表迭代器 二.前序遍历(无需访问非叶节点) 1.递归实现 2.栈模拟 3.惰性栈模拟(next时再入栈) 总结 参考文献 前言 问题转换:将需求确认,结合已 ...

  4. leetcode 341. 扁平化嵌套列表迭代器(dfs)

    给你一个嵌套的整型列表.请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 列表中的每一项或者为一个整数,或者是另一个列表.其中列表的元素也可能是整数或是其他列表. 示例 1: 输入: [[1 ...

  5. 每日一题:leetcode341.扁平化嵌套列表迭代器

    题目描述 题目分析 这个题目自己大概花了一个小时,虽然是一遍AC,但是速度有点慢,太长时间不写代码导致自己对代码不太敏感,写起来慢腾腾的. 看到这个的想法就是,要用栈来保存列表的迭代器,这样将孩子列表 ...

  6. LeetCode 341. 扁平化嵌套列表迭代器(双栈)

    文章目录 1. 题目 2. 双栈解题 1. 题目 给定一个嵌套的整型列表.设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 列表中的项或者为一个整数,或者是另一个列表. 示例 1: 输入: [[ ...

  7. leetcode算法题--扁平化嵌套列表迭代器

    题目链接:https://leetcode-cn.com/problems/flatten-nested-list-iterator/ 这个题目不难,就是像树的深度遍历,就是题目有点难理解. /*** ...

  8. LeetCode Flatten Nested List Iterator

    问题:设计一个嵌套的迭代器.容器内的元素为NestedInteger,可以是一个整数,也可以是一个整数数组. 思路:使用两个栈,aidStack存储NestedInteger,ansStack存储In ...

  9. LeetCode Flatten Nested List Iterator(栈)

    问题:给出一个嵌套的整型列表.设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 思路:使用两个栈,一个存放结果栈,一个是中间辅助栈.先将列表放入中间辅助栈.然后将中间辅助栈出栈时,如果栈顶元素是 ...

最新文章

  1. 联合索引最左匹配原则成因
  2. 皮一皮:据说这句话高情商的人说起来不一样...
  3. java 文件 剪切_总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇
  4. C语言程序设计之编程求鸡和兔的只数,用穷举法解决
  5. Python 输入与输出
  6. fileinputstream resources 读取文件_压缩20M文件从30秒到1秒,包教包会
  7. 讲oracle个阶段书籍6,经典图书推荐系列-之六
  8. flo file_Flo菜单简介:可扩展的拇指友好型移动导航
  9. 尝鲜!.NET5实操之docker+k8s,这10个坑,你不得不知!
  10. 表头合并_多个Excel表格合并数据麻烦?试试Power Query轻松帮你解决
  11. 机制 linux_从一道面试题谈linux下fork的运行机制
  12. 《解读NoSQL》——1.4 小结
  13. 基于CSS3的3D旋转效果
  14. 2014百度之星资格赛—— Xor Sum(01字典树)
  15. RHEL6.3 NFS服务搭建过程详解
  16. #读取json某一节点数据_06596.2.0Hive处理JSON格式数据
  17. 日本现代数学发展历程及其启示
  18. HTML语法结构及规范
  19. 安卓10源码开发定制(21)GPS定位研究(3)修改GPS定位数据测试gps定位代码
  20. 让架构接地气,不再云里雾里

热门文章

  1. 2019ICPC(徐州) - Who is better?(中国剩余定理+斐波那契博弈)
  2. POJ - 3660 Cow Contest(最短路变形+闭包传递)
  3. python输出奇数数字序位_python对输出的奇数偶数排序实例代码
  4. PIL模块与随机生成中文验证码
  5. 深度解密之HDU3826(Square free number)
  6. Rustup 管理工具
  7. 一个python发包的脚本
  8. 【Boost】boost库中thread多线程详解3——细说lock_guard
  9. Cocos2d-x创建包边字
  10. 从容器到容器云,什么才是 Kubernetes 的本质?