341. 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]
.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道.next 和 .hasnext有啥区别:取出来、只是看看有没有
[一句话思路]:
只有stack才能一次取出来一层,getlist getinteger
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- .next建立在.hasnext的基础之上,所以hasnext需要放在while循环中,做完为止
- 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展开多层数组的更多相关文章
- [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 ...
- 341. Flatten Nested List Iterator
List里可以有int或者List,然后里面的List里面可以再有List. 用Stack来做比较直观 Iterator无非是next()或者hasNext()这2个方程 一开始我想的是hasNext ...
- [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/
- [LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- Leetcode: Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
随机推荐
- Oracle12c之 CDB数据库中数据字典架构
数据字典就是元数据的集合,比如创建的表,列,约束,触发器等等这些都是元数据,需要保存到数据库中.除此之外,Oracle自身的一些数据库对象,如目录,PL/SQL代码等等这些都是元数据,都需要存放在数据 ...
- oracle之 11g RAC R2 体系结构---Grid
-- 查看cluster 所维护的资源列表,不包括 OHAS 栈的 daemon [root@node1 bin]# ./crsctl status resource -t-------------- ...
- C语言 数组排序法总结
//快速排序法 void QuickSort(int *a, int left, int right) { int i = left; int j = right; int k = a[left]; ...
- Nginx优化指南
大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了!而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...
- MySQL添加数据库的唯一索引的几种方式~
创建表时直接设置: DROP TABLE IF EXISTS `student`;CREATE TABLE `student` ( `stu_id` int(11) NOT NULL AUTO_IN ...
- linux下echo命令详解
linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法 例如: echo $JAVA_HOME /export/se ...
- 运维平台cmdb开发-day1
序读项目由来 终极目标,运维平台.自动化.装机,监控,安装软件,部署基础服务,资产管理,之前是excel,现在是客户端自动获取,变更记录 后台管理 api 采集资产 四种模式agent 定时,每天执行 ...
- node+express+socket.io制作一个聊天室功能
首先是下载包: npm install express npm install socket.io 建立文件: 服务器端代码:server.js var http=require("http ...
- openstack网络架构(nova-network/neutron)
openstack网络体系中,网络技术没有创新,但用到的技术点非常庞杂,包括bridge.vlan.gre.vxlan.ovs.openflow.sdn.iptables等,当然这里不会做具体技术介绍 ...
- apt-get使用国内镜像源
apt-get 1.复制原文件备份(万一弄坏源文件可恢复) sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak 2.编辑源列表文件 sudo ...