[leetcode]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:
Input: [[1,1],2,[1,1]]
Output: [1,1,2,1,1]
Explanation: By calling next repeatedly until hasNext returns false,
the order of elements returned by next should be:[1,1,2,1,1].
Example 2:
Input: [1,[4,[6]]]
Output: [1,4,6]
Explanation: By calling next repeatedly until hasNext returns false,
the order of elements returned by next should be:[1,4,6].
题目
将Nested List展平,像访问一个一维列表一样访问嵌套列表。
思路
queue
代码
public class NestedIterator implements Iterator<Integer> {
Queue<Integer> queue;
public NestedIterator(List<NestedInteger> nestedList) {
queue = new LinkedList<>();
helper(nestedList);
}
@Override
public Integer next() {
return queue.poll();
}
@Override
public boolean hasNext() {
return !queue.isEmpty();
}
private void helper(List<NestedInteger> nestedList){
for(NestedInteger n:nestedList){
if(n.isInteger()){
queue.add(n.getInteger());
}else{
helper(n.getList());
}
}
}
}
[leetcode]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 ...
- 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】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 a ...
- 341. Flatten Nested List Iterator
List里可以有int或者List,然后里面的List里面可以再有List. 用Stack来做比较直观 Iterator无非是next()或者hasNext()这2个方程 一开始我想的是hasNext ...
- Python代码阅读(第11篇):展开嵌套列表
Python 代码阅读合集介绍:为什么不推荐Python初学者直接看项目源码 本篇阅读的代码实现了展开嵌套列表的功能,将一个嵌套的list展开成一个一维list(不改变原有列表的顺序). 本篇阅读的代 ...
随机推荐
- algernon 基于golang 的独立的支持redis lua pg。。。 的web server
algernon 看到github 的介绍很很强大,一下子想到了openresty,功能看着很强大,支持 redis pg lua markdown quic http2 mysql 限速 pongo ...
- 集合总结三(HashMap的实现原理)
一.概述 二话不说,一上来就点开源码,发现里面有一段介绍如下: Hash table based implementation of the Map interface. This implement ...
- 我发起并创立了一个 C 语言编译器 开源项目 InnerC
本文是 VMBC / D# 项目 的 系列文章, 有关 VMBC / D# , 见 <我发起并创立了一个 VMBC 的 子项目 D#>(以下简称 <D#>) https: ...
- WPF项目中解决ConfigurationManager不能用(转)
https://blog.csdn.net/MOESECSDN/article/details/78107888 在WPF项目中遇到这样的问题,做一下笔记.希望对自己和读者都有帮助. 在aap.con ...
- AIOps指导
AIOps代表运维操作的人工智能(Artificial Intelligence for IT Operations), 是由Gartner定义的新类别,Gartner的报告宣称,到2020年, ...
- 前段开发神奇webstorm安装注册和汉化
软件下载地址: http://www.jetbrains.com/webstorm/ 安装完后退出. 重新打开,进行激活 这里我们选择“license server”然后输入:http://idea. ...
- 用C自撸apache简易模块,搭建图片处理服务器。
写C是个撸sir /* ** mod_acthumb.c -- Apache sample acthumb module ** [Autogenerated via ``apxs -n acthumb ...
- Flask 框架
装饰器知识回顾 http://www.cnblogs.com/0bug/p/7978595.html 普通装饰器格式: def wrapper(func): def inner(*args, **kw ...
- 20165312 2017-2018-2《JAVA程序设计》第7周学习总结
20165312 2017-2018-2<JAVA程序设计>第7周学习总结 一.对上周测试的查漏补缺 总的来说,我觉得上周两个测试都挺难的,做测试也花费了很长的时间,我认为是因为书上的知识 ...
- minicom 抓取log
使用minicom也有很长时间了,只用minicom抓过uart log,但是从来没有去保存过这个log,也不知道有这个功能.后来在超级终端中发现有这个功能(传送->捕获文字),想想minico ...