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.

Example 1:
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]. Example 2:
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].

非常精巧地使用stack。push all the nestedList into the stack from back to front,so when we pop the stack, it returns the very first element

执行hasNext()的时候,如果peek()是integer, return true;否则是一个List<NestedInteger>, 则flatten这个list,同样的,把list里的NestedInteger倒序push入栈

 /**
* // 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();
* }
*/
public class NestedIterator implements Iterator<Integer> {
Stack<NestedInteger> st; public NestedIterator(List<NestedInteger> nestedList) {
st = new Stack<NestedInteger>();
for (int i=nestedList.size()-1; i>=0; i--) {
st.push(nestedList.get(i));
}
} @Override
public Integer next() {
return st.pop().getInteger();
} @Override
public boolean hasNext() {
while (!st.isEmpty()) {
if (st.peek().isInteger()) return true;
List<NestedInteger> cur = st.pop().getList();
for (int i=cur.size()-1; i>=0; i--) {
st.push(cur.get(i));
}
}
return false;
}
} /**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/

Leetcode: Flatten Nested List Iterator的更多相关文章

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

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

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

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

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

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

  4. 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...

  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. 【leetcode】341. Flatten Nested List Iterator

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

  7. LeetCode 341. Flatten Nested List Iterator

    https://leetcode.com/problems/flatten-nested-list-iterator/

  8. [Swift]LeetCode341. 压平嵌套链表迭代器 | Flatten Nested List Iterator

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

  9. Flatten Nested List Iterator

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

随机推荐

  1. fenshijin

    #include<stdio.h> int map[6][4]={8,0,18,10,      13,10,15,20,      10,50,13,30,               ...

  2. for循环经典案例

    1.棋盘放粮食棋盘有32个格子,第一个格子放1个芝麻,第二个放2个,第三个放4个,第四个放8个...每个芝麻的重量为0.00001kg,如果要放满整个棋盘,需要多少重量的芝麻. <!DOCTYP ...

  3. Qt与VS2005/2008的完美配合(自己编译Qt4.5.1的详细步骤)

    介绍 用过Linux的人想必都知道Qt(不是QuickTime,呵呵)这个名称,KDE就是用Qt写的,我也是接触Linux后才认识它的. Qt原先是奇趣科技TM(Trolltech)的产品,由Haav ...

  4. linux epoll 简单demo

    一个简单的epoll demo ,同时接受多个客户端连接,并把接收到的字符串转化为大写字母返回给客户端 #include<stdio.h> #include<arpa/inet.h& ...

  5. php--.prop()

    .prop() 获取匹配的元素集中第一个元素的属性(property)值或设置每一个匹配元素的一个或多个属性. .prop()方法只获得第一个匹配元素的属性值 .如果元素上没有该属性,或者如果没有匹配 ...

  6. VS2013修改MVC4默认生成的模板

    找到以下目录,根据VS版本和安装目录不同相应改动: I:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTempla ...

  7. WIN 8.1使用常见问题及解决

      WIN 8.1正式版64位(cn_windows_8_1_pro_vl_x64_dvd_2971907)使用问题及解决 一.IE11的输入框会变蓝: 如在百度中搜索时,搜索框和按钮均变成蓝色背景, ...

  8. LightOj1007 - Mathematically Hard(欧拉函数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1007 题意:给你两个数a和b,求他们之间所有数的欧拉值得平方和; a and b (2 ...

  9. CocoaPods的安装及使用/利用开源库Diplomat实现分享及第三方登录/git的使用

    <<史上最简洁版本>> 1.gem sources -l查看 当前的源 //1.1 sudo -i..以下都是以管理员的身份来操作的 2.gem sources --remov ...

  10. Hashtable,HashMap,Dictionary的区别

    Hashtable和HashMap的区别:1.Hashtable是基于Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现,c#中无HashMap2.Hashtable ...