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. three.js 制作一个简单的圆柱体模型

    <!DOCTYPE html> <html lang="en"> <head> <title>three.js webgl - or ...

  2. 雷林鹏分享:XML 元素

    XML 元素 XML 文档包含 XML 元素. 什么是 XML 元素? XML 元素指的是从(且包括)开始标签直到(且包括)结束标签的部分. 一个元素可以包含: 其他元素 文本 属性 或混合以上所有. ...

  3. English Voice of <<All Of Me>>

    "All Of Me"我的一切 [Verse 1:]What would I do without your smart mouth没有你的蜜语甜言,我该怎办Drawing me ...

  4. es的mapping设置

    自定义mapping的api PUT test_index { "mappings": { #mappings关键字 "doc": { #type " ...

  5. MySQL事务(三)

    一.事务(Innodb锁)的隔离级别概述 并发事务带来的问题: 更新丢失(lost update):当两个或多个事务选择同一行,然后基于最初选定的值更新该行时,由于每个事务都不知道其他事务的存在,就会 ...

  6. 胜利大逃亡 HDU - 1253

    Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0 ...

  7. git 提交代码场景

    1.首次提交代码到master git init git add . git commit -m '' git remote add origin +address git push -u origi ...

  8. bzoj3676: [Apio2014]回文串 pam

    题意:字符串s.我们定义s的一个子串t的"出 现值"为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. 题解:pam板子题 //cnt数组表示该节点代表的 ...

  9. 【MySQL】【3】String和Date相互转换

    正文: 1,Date转String --结果:<2019-04-10> SELECT DATE_FORMAT(SYSDATE(), "%Y-%m-%d") FROM D ...

  10. laravel请求到响应的生命周期

    请求到响应的核个执行过程,主要可以归纳为四个阶段,即程序启动准备阶段.请求实例化阶段.请求处理阶段.响应发送和程序终止阶段. public\index.php中有这么一段代码 $app = requi ...