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. String的intern()方法和java关键字、保留字

    String s1 = new StringBuilder("hel").append("lo").toString(); //hello System.out ...

  2. 【源码分析】FastJson全局配置日期格式导致@JSONField(format = "yyyy-MM-dd")注解失效

    出现的问题 我全局配置的时间格式是:yyyy-MM-dd HH:mm:ss @JSONField注解配置的时间格式是:yyyy-MM-dd 最终的返回结果是:yyyy-MM-dd HH:mm:ss 问 ...

  3. Vue.js的后端数据支持:使用Express建立app, 并使用MongoDB数据库。

    需要用到的backed tech stack: Node: JavaScript on the server/backend. That's basically what it is, but mor ...

  4. Selenium自动化测试框架入门整理

    ​​关注嘉为科技,获取运维新知 本文主要针对Selenium自动化测试框架入门整理,只涉及总体功能及框架要点介绍说明,以及使用前提技术基础要求整理说明.作为开发人员.测试人员入门参考. 本文参考:Se ...

  5. MyEclipse6.5的SVN插件的安装

    在线安装 1. 打开Myeclipse,在菜单栏中选择Help→Software Updates→Find and Install; 2. 选择Search for new features to i ...

  6. scrapy 爬虫框架(一)

    一 . scrapy 的安装 安装scrapy框架时,需要先安装依赖包. #Linux: pip3 install scrapy #Windows: a. pip3 install wheel b. ...

  7. Xpath做数据解析

    xpath是一个路径表达式, xpath学习 (1)xpath节点 在XPath中,有七种类型的节点:元素,属性,文本,命名空间,处理指令,注释以及文档节点:XML文档是被作为节点树来对待的.树的根被 ...

  8. python基础之函数名的使用,闭包以及迭代器

    内容梗概: 1. 函数名的使⽤用以及第⼀一类对象 2. 闭包 3. 迭代器 1.函数名一. 函数名的运⽤.函数名是一个变量, 但它是⼀个特殊的变量, 与括号配合可以执行函数的变量.1.1 函数名的内存 ...

  9. 为什么样本方差除以(n-1)而不是n ?(自由度)

    不记得第几次看见样本方差的公式,突然好奇为什么要除以(n-1)而不是n呢?看见一篇文章从定义上和无偏估计推导上讲的很清楚https://blog.csdn.net/fuming2021118535/a ...

  10. Leetcode 124 *

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...