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. java ReentrantReadWriteLock

    // read and write lock is mutual exclusion lock //Listing 7-3. Using ReadWriteLock to Satisfy a Dict ...

  2. oj 1031 random permutation

    Problem A: Random Permutations Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 91  Solved: 54 Descri ...

  3. discuz函数解析--写日志

    public static function writelog($file, $log) { global $_G; $yearmonth = dgmdate(TIMESTAMP, 'Ym', $_G ...

  4. 设计模式:命令模式(Command)

    定   义:将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排列或者记录请求日志,以及支持可撤销的操作. 结构图: Command类: abstract class Comma ...

  5. 实验 snort安装配置与规则编写

    1 实验目的 在linux或windows任意一个平台下完成snort的安装,使snort工作在NIDS模式下,并编写符合相关情景要求的snort规则. 2 实验环境 物理机:windows 8.1 ...

  6. Magento white screen or how XML can break your site?

    Magento white screen or how XML can break your site? by SANDO on 02. OCT, 2012 in MAGENTO, SMALL TIP ...

  7. GCD Demo (先存起来自己看)

    // 原代码块一 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // 原代码块二 N ...

  8. 使用jQuery为表单添加回车事件

    $(document).keypress(function(e){ if(e.which==13){ checkUserForm(); } });

  9. WordPress小工具开发教程(网站公告)

    WordPress小工具开发教程(网站公告) BY TIANQIXIN · 2012 年 12 月 26 日   wordpress主题小工具,可以自由拖动到侧边栏,并在前台实现相应功能!一般自带的小 ...

  10. git的简单使用

    Git-csm.com 安装git  http://www.git-scm.com/download [liujianzuo@mylab ~]$ yum install git 已加载插件:faste ...