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. ico 图标 生成 工具 网站

    http://www.faviconico.org/favicon favicon.ico在线制作,在线Favicon.ico制作转换工具

  2. 上传RNA-seq数据到NCBI GEO数据库

    SRA - NCBI example - NCBI 要发文章了,审稿时编辑肯定会要求你上传NGS测序数据. 一般数据都是放在集群,不可能放在个人电脑上,因为有的数据大的吓人(几个T). 所以我们就建一 ...

  3. 【洛谷p1313】计算系数

    (%%%hmr) 计算系数[传送门] 算法呀那个标签: (越来越懒得写辽)(所以今天打算好好写一写) 首先(ax+by)k的计算需要用到二项式定理: 对于(x+y)k,有第r+1项的系数为:Tr+1= ...

  4. Integer To Roman leetcode java

    问题描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  5. Web 自动化测试框架 sweetest 介绍

    项目开源: https://github.com/tonglei100/sweetest 文章转载:https://segmentfault.com/a/1190000011612061 介绍 swe ...

  6. grid的简单使用

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>grid ...

  7. PinkEx专用图标

  8. git设置远程同步分支

    git push --set-upstream origin yourBranchName

  9. InnoDB存储引擎的 B+ 树索引

    B+ 树是为磁盘设计的 m 叉平衡查找树,在B+树中,所有的记录都是按照键值的大小,顺序存放在同一层的叶子节点上,各叶子节点组成双链表.叶节点是数据,非叶节点是索引. 首先,需要清楚:B+ 树索引并不 ...

  10. Auto Encoder用于异常检测

    对基于深度神经网络的Auto Encoder用于异常检测的一些思考 from:https://my.oschina.net/u/1778239/blog/1861724 一.前言 现实中,大部分数据都 ...