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
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].

LeetCode上的原题,请参见我之前的博客Flatten Nested List Iterator。但是不太明白的是,那篇博客中的解法三可以通过LeetCode的OJ,在LintCode上跑就有错误,不知道啥原因。

解法一:

  1. class NestedIterator {
  2. public:
  3. NestedIterator(vector<NestedInteger> &nestedList) {
  4. for (int i = nestedList.size() - ; i >= ; --i) {
  5. s.push(nestedList[i]);
  6. }
  7. }
  8.  
  9. int next() {
  10. NestedInteger t = s.top(); s.pop();
  11. return t.getInteger();
  12. }
  13.  
  14. bool hasNext() {
  15. while (!s.empty()) {
  16. NestedInteger t = s.top();
  17. if (t.isInteger()) return true;
  18. s.pop();
  19. for (int i = t.getList().size() - ; i >= ; --i) {
  20. s.push(t.getList()[i]);
  21. }
  22. }
  23. return false;
  24. }
  25.  
  26. private:
  27. stack<NestedInteger> s;
  28. };

解法二:

  1. class NestedIterator {
  2. public:
  3. NestedIterator(vector<NestedInteger> &nestedList) {
  4. for (auto a : nestedList) {
  5. d.push_back(a);
  6. }
  7. }
  8.  
  9. int next() {
  10. NestedInteger t = d.front(); d.pop_front();
  11. return t.getInteger();
  12. }
  13.  
  14. bool hasNext() {
  15. while (!d.empty()) {
  16. NestedInteger t = d.front();
  17. if (t.isInteger()) return true;
  18. d.pop_front();
  19. for (int i = ; i < t.getList().size(); ++i) {
  20. d.insert(d.begin() + i, t.getList()[i]);
  21. }
  22. }
  23. return false;
  24. }
  25.  
  26. private:
  27. deque<NestedInteger> d;
  28. };

[LintCode] 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. [LeetCode] 341. Flatten Nested List Iterator 压平嵌套链表迭代器

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

  3. [Swift]LeetCode341. 压平嵌套链表迭代器 | 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展开嵌套列表的迭代器

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

  5. [LeetCode] Nested List Weight Sum 嵌套链表权重和

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

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

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

  7. Leetcode: Flatten Nested List Iterator

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

  8. Flatten Nested List Iterator

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

  9. Design-341. Flatten Nested List Iterator

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

随机推荐

  1. Sql Server 基础知识

    Sql Server 基础知识: http://blog.csdn.net/t6786780/article/details/4525652 Sql Server 语句大全: http://www.c ...

  2. Linux内核system_call中断处理过程

    “平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” men ...

  3. 纯window下VMware 安装 OS X El Capitan 原版映像【未完待续】

    一.所需软件1.下载OS X El Capitan 10.11.2 15C50链接:http://pan.baidu.com/s/1skuLgAx 密码:u2jf 2.下载VMware Worksta ...

  4. SQL.WITH AS.公用表表达式(CTE)(转)

    一.WITH AS的含义    WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是 ...

  5. directive中的参数详解

    restrictE: 表示该directive仅能以element方式使用,即:<my-dialog></my-dialog>A: 表示该directive仅能以attribu ...

  6. poj3616 LIS变形

    题目链接:http://poj.org/problem?id=3616 题意:给出m组数据a,b,c代表在第a分钟到第b分钟产生c个效益,问最大产生多少效益(区间不能重叠,每次工作完必须歇息R分钟) ...

  7. BZOJ 2342 回文串-Manacher

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2342 思路:先跑一遍Manacher求出p[i]为每个位置为中心的回文半径,因为双倍回文串 ...

  8. BZOJ 2648 SJY摆棋子 ——KD-Tree

    [题目分析] KD-Tree第一题,其实大概就是搜索剪枝的思想,在随机数据下可以表现的非常好NlogN,但是特殊数据下会达到N^2. 精髓就在于估价函数get以及按照不同维度顺序划分的思想. [代码] ...

  9. css3 -- 多列

    1.指定分列: E{column-count:2:} --- 两列 E{ -moz-column-count:2: -webkit-column-count:2: } Firefox与webkit实现 ...

  10. Git分支操作

    1.创建分支 git branch <分支名> 2.切换分支 git checkout <分支名> 该语句和上一个语句可以和起来用一个语句表示: git checkout -b ...