[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道.next 和 .hasnext有啥区别:取出来、只是看看有没有

[一句话思路]:

只有stack才能一次取出来一层,getlist getinteger

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. .next建立在.hasnext的基础之上,所以hasnext需要放在while循环中,做完为止
  2. curr如果是个list,就必须用专有方法
    curr.getList()

    先取出,再做后续操作

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

一次放一层

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

list 对应的方法是.size() .get

只有stack才能一次取出来一层,数组不能直接取出来一层。所以用stack。

stack有

.getInteger()
.getList()

方法

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

public NestedIterator(List<NestedInteger> nestedList) {
//put into stack from back
for (int i = nestedList.size() - 1; i >= 0; i--) stack.push(nestedList.get(i));
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

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

341. Flatten Nested List Iterator展开多层数组的更多相关文章

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

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

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

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

  3. 341. Flatten Nested List Iterator

    List里可以有int或者List,然后里面的List里面可以再有List. 用Stack来做比较直观 Iterator无非是next()或者hasNext()这2个方程 一开始我想的是hasNext ...

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

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

  5. 【leetcode】341. Flatten Nested List Iterator

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

  6. LeetCode 341. Flatten Nested List Iterator

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

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

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

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

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

  9. Leetcode: Flatten Nested List Iterator

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

随机推荐

  1. Android.mk语法解析

    Android.mk 相当于 Linux 中的 Makefile 文件,用来向安卓系统描述如何编译源代码.该文件会被编译器解析多次,所以尽量减少在 Android.mk 中声明变量. Android. ...

  2. 可以随时查找的max栈和max队列——面试

    这是面试中比较常见的题目,max队列也是编程之美里的一道题 对于max的栈,有个比较简单的办法就是,每次入栈前判断栈顶元素与正在入栈的元素哪个大,哪个大就哪个入栈 对于队列,我们知道可以用两个栈来实现 ...

  3. OpenSSL生成公钥私钥***

    证书标准 X.509 - 这是一种证书标准,主要定义了证书中应该包含哪些内容.其详情可以参考RFC5280,SSL使用的就是这种证书标准. 编码格式 同样的X.509证书,可能有不同的编码格式,目前有 ...

  4. 使用BeanShell 对比取出来的值

    描述: 使用BeanShell 对比取出来的值,如不一致,报错 步骤一: 使用json Extractor后置处理器,取出"登入成功" 使用BeanS hell断言: 语法: if ...

  5. 第三章 Istio基本介绍

    3.1 Istio的核心组件及其功能 Istio总体分两部分:控制面和数据面. 数据面(sidecar):sidecar通过注入的方式和业务容器共存于一个pod,会劫持业务容器的流量,并接受控制面组件 ...

  6. 使用Ajax异步上传文件

    之前上传文件都是用表单form设置post请求和enctype类型: <form id="upload_form"action="" method=&qu ...

  7. 安装FFmpeg3.0.9

    //静态版的 FFmpeg Static Builds   release: 3.3.3 https://www.johnvansickle.com/ffmpeg/ ffmpeg-release-64 ...

  8. windows zabbix_agent 客户端安装部署

    1.下载客户端:zabbix_agentd.zip 2.在c盘创建文件夹zabbix,解压conf和bin目录 3.将conf下的zabbix_agentd.win.conf 修改为zabbix_ag ...

  9. C# 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke [问题点数:40分

    注意:  this.DateTimeRun = true;            new Thread(jishi_kernel).Start(); 线程的启动,不能放在    public Form ...

  10. leetcode706

    class MyHashMap { public: vector<int> hashMap; /** Initialize your data structure here. */ MyH ...