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的更多相关文章

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

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

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

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

  3. Leetcode: Flatten Nested List Iterator

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

  4. [Swift]LeetCode341. 压平嵌套链表迭代器 | 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 an inte ...

  6. Design-341. Flatten Nested List Iterator

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

  7. 341. Flatten Nested List Iterator展开多层数组

    [抄题]: Given a nested list of integers, implement an iterator to flatten it. Each element is either a ...

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

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

  9. 【leetcode】341. Flatten Nested List Iterator

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

随机推荐

  1. 昂达 v891 v1 终于 删除 windows 分区 并且恢复了容量。

    参考了很多文章(最后列出重要的),却始终失败. 途中因为乱改分区表,竟然fastboot 都进不去了,当时真是欲哭无泪. 总结关键点: 1) partition.tbl不能把硬盘剩余空间全给data分 ...

  2. breakthroughs in statistics | 统计学历史

    <breakthroughs in statistics>- 这本书理解透了,统计方面应该可以封神了. 亚马逊上有卖,貌似还有好几卷. Breakthroughs in Statistic ...

  3. 2017-2018-2 20165303 实验二《Java面向对象程序设计》实验报告

    实验一 实验要求 参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习 提交最后三个JUnit测试用例(正常情况, ...

  4. 安装EF实体模型框架

    Data Access and Storage > 学习 > Entity Framework > 开始操作 > 空间 - EF 设计器 本视频和分步演练介绍如何使用实体框架设 ...

  5. 矩阵最优路线DP

    母题:矩阵中每个点有权值,每经过一个点就累加权值,求从a点到b点的最优(最大)路线. 题型1: 从左上到右下,只能向下或者向右 for 行 for 列 dp=max dp左,dp上; 扫一遍就行 有时 ...

  6. hadoop ssh 端口-ssh-copy-id详解

    ssh-copy-id详解 http://www.blogdaren.com/post-1815.html 服务器时常需要配置无密码的登录方式,最一般的设置方式如下: 使用ssh-keygen和ssh ...

  7. Card Game Again CodeForces - 818E (双指针)

    大意: 给定序列, 求多少个区间积被k整除. 整除信息满足单调性, 显然双指针. 具体实现只需要考虑k的素数向量, 对每一维维护个指针即可. 这题看了下cf其他人的做法, 发现可以直接暴力, 若当前的 ...

  8. 384. Shuffle an Array(java,数组全排列,然后随机取)

    题目: Shuffle a set of numbers without duplicates. 分析: 对一组不包含重复元素的数组进行随机重排,reset方法返回最原始的数组,shuffle方法随机 ...

  9. java进行url编码和解码

    public static String getURLEncoderString(String str) { String result = ""; if (null == str ...

  10. python-前20天的着重知识点

    1.CPU存在两种工作状态:一种是内核态,操作系统在运行--可以操作硬件: 另一种是用户态,是应用软件在运行--不可以操作硬件. 应用软件要控制硬件,就要从用户态切换成内核态 2.多道技术:(多道指的 ...