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.
Notice You don't need to implement the remove method.
Example
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].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].
题目中的NestedInteger应该是实际当中General Grouping Object (or Container)的特例。看到这种数据结构的第一反应就是用stack。注意倒叙插入即可。
/**
* // 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();
* }
*/
import java.util.Iterator; public class NestedIterator implements Iterator<Integer> { Stack<NestedInteger> stack;
public NestedIterator(List<NestedInteger> nestedList) {
// Initialize your data structure here.
stack = new Stack<NestedInteger>();
if(nestedList!=null){
for(int i=nestedList.size()-1;i>=0;i--){
stack.push(nestedList.get(i));
}
}
} // @return {int} the next element in the iteration
@Override
public Integer next() {
// Write your code here
return stack.pop().getInteger();
} // @return {boolean} true if the iteration has more element or false
@Override
public boolean hasNext() {
// Write your code here
while(!stack.empty()){
NestedInteger cur = stack.peek();
if(cur.isInteger()) return true;
stack.pop();
List<NestedInteger> list = cur.getList();
if(list!=null){
for(int i=list.size()-1;i>=0;i--){
stack.push(list.get(i));
}
}
}
return false;
} @Override
public void remove() {}
} /**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v.add(i.next());
*/
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 ...
- [Swift]LeetCode341. 压平嵌套链表迭代器 | 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 an inte ...
- Design-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 压平嵌套链表迭代器
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 ...
随机推荐
- python logging日志输出个文件中
# -*- coding:utf-8 -*- import logging # 引入logging模块 import os.path import time # 第一步,创建一个logger logg ...
- mq/mysql/redis/nginx常见服务&工具安装
单机版 3.1安装工具 3.1.1 安装Maven工具 3.1.1上传安装包 1)root用户创建安装目录如/usr/local /maven: mkdir -p /usr/local/maven ...
- linux下编译C/C++ 程序
C/C++的速度是Python和perl所无法比拟的,尤其对于处理超大的生物信息学文件来说. 最近在写一个最简单的fastq cut工具,Python简直慢到不能忍,8G的fastq.gz文件的cut ...
- 20165303 2017-2017-2《Java程序设计》课程总结
1.我期待的师生关系 20165303我期望的师生关系 2.学习基础和C语言基础调查 20165303学习基础和C语言基础调查 3.linux安装及学习 20165303 预备作业3 Linux安装及 ...
- Juniper基础配置
root> show configuration | display set 配置按set行显示,查看的配置为未commit的配置(commit check)root# set sys ...
- LeetCode--409--最长回文串
问题描述: 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字 ...
- Confluence 6 自定义空间布局
你可以通过编辑布局文件来对 Confluence 的外观和表现进行编辑.这个页面将会告诉你如何来为空间自定义布局文件.你需要系统管理员的 全局权限(global permission) 和你希望进行修 ...
- Mysql 中如何创建数据库和数据表
这里的数据库为:user 数据表为 aaa mysql –uroot –p 进入mysql create database user; 创建数据 ...
- CF1129C Morse Code
pro: 维护一个01字符串,支持在结尾动态加字符. 每一个长度<=4的01串可以对应一个字母(有几个特例除外) 每次操作后询问,这个字符串的所有子串一共可以对应出多少种本质不同的字符串. so ...
- 并查集 牛客练习赛41 C抓捕盗窃犯
题目链接 :https://ac.nowcoder.com/acm/contest/373/C 题意,初始每一个城市都有一伙盗贼,没过一个时刻盗贼就会逃窜到另一个城市,你可以在m个城市设置监察站,会逮 ...