给定一个嵌套的整型列表。设计一个迭代器,使其能够遍历这个整型列表中的所有整数。

列表中的项或者为一个整数,或者是另一个列表。

示例 1:

输入: [[1,1],2,[1,1]]
输出: [1,1,2,1,1]
解释: 通过重复调用 next 直到 hasNext 返回false,next 返回的元素的顺序应该是: [1,1,2,1,1]。

示例 2:

输入: [1,[4,[6]]]
输出: [1,4,6]
解释: 通过重复调用 next 直到 hasNext 返回false,next 返回的元素的顺序应该是: [1,4,6]。

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * class NestedInteger {
 *   public:
 *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     bool isInteger() const;
 *
 *     // Return the single integer that this NestedInteger holds, if it holds a single integer
 *     // The result is undefined if this NestedInteger holds a nested list
 *     int getInteger() const;
 *
 *     // Return the nested list that this NestedInteger holds, if it holds a nested list
 *     // The result is undefined if this NestedInteger holds a single integer
 *     const vector<NestedInteger> &getList() const;
 * };
 */
class NestedIterator {
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        for (int i = nestedList.size() - 1; i >= 0; --i) {
            s.push(nestedList[i]);
        }
    }

int next() {
        NestedInteger t = s.top(); s.pop();
        return t.getInteger();
    }

bool hasNext() {
        while (!s.empty()) {
            NestedInteger t = s.top(); 
            if (t.isInteger()) return true;
            s.pop();
            for (int i = t.getList().size() - 1; i >= 0; --i) {
                s.push(t.getList()[i]);
            }
        }
        return false;
    }

private:
    stack<NestedInteger> s;
};

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i(nestedList);
 * while (i.hasNext()) cout << i.next();
 */

341.扁平化嵌套列表迭代器相关推荐

  1. leetcode 341. Flatten Nested List Iterator | 341. 扁平化嵌套列表迭代器(Java)

    题目 https://leetcode.com/problems/flatten-nested-list-iterator/ 这标题,翻译的是人话吗?啥叫扁平化嵌套列表迭代器?.. 题解 比较像深度优 ...

  2. 341. 扁平化嵌套列表迭代器

    2020-05-24 1.题目描述 扁平化嵌套列表迭代器 2.题解 使用递归即可 3.代码 /*** // This is the interface that allows for creating ...

  3. leetcode 341. 扁平化嵌套列表迭代器(dfs)

    给你一个嵌套的整型列表.请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 列表中的每一项或者为一个整数,或者是另一个列表.其中列表的元素也可能是整数或是其他列表. 示例 1: 输入: [[1 ...

  4. LeetCode 341. 扁平化嵌套列表迭代器(双栈)

    文章目录 1. 题目 2. 双栈解题 1. 题目 给定一个嵌套的整型列表.设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 列表中的项或者为一个整数,或者是另一个列表. 示例 1: 输入: [[ ...

  5. 力扣--扁平化嵌套列表迭代器

    扁平化嵌套列表迭代器 文章目录 扁平化嵌套列表迭代器 一.题目描述 二.分析 方法一: 代码一: 方法二: 代码二: C++代码: 一.题目描述 /*** // This is the interfa ...

  6. 扁平化嵌套列表迭代器 [树的递归前序遍历 + 迭代前序遍历]

    递归|迭代的前序遍历 前言 一.扁平化嵌套列表迭代器 二.前序遍历(无需访问非叶节点) 1.递归实现 2.栈模拟 3.惰性栈模拟(next时再入栈) 总结 参考文献 前言 问题转换:将需求确认,结合已 ...

  7. 每日一题:leetcode341.扁平化嵌套列表迭代器

    题目描述 题目分析 这个题目自己大概花了一个小时,虽然是一遍AC,但是速度有点慢,太长时间不写代码导致自己对代码不太敏感,写起来慢腾腾的. 看到这个的想法就是,要用栈来保存列表的迭代器,这样将孩子列表 ...

  8. leetcode算法题--扁平化嵌套列表迭代器

    题目链接:https://leetcode-cn.com/problems/flatten-nested-list-iterator/ 这个题目不难,就是像树的深度遍历,就是题目有点难理解. /*** ...

  9. Python花式编程:多层嵌套列表扁平化

    封面图片:<Python程序设计基础(第2版)>,ISBN:9787302490562,董付国,清华大学出版社 图书详情: 好消息:智慧树网APP"知到"中搜索&quo ...

  10. Python使用递归对任意嵌套列表进行扁平化

    首先补充一个地方,之前有个文章演示的是猜数游戏,原文链接为猜数游戏用Python应该这样写,代码中漏掉了一个break语句,也就是说,在猜对的时候输出语句print('Congratulations! ...

最新文章

  1. SEO:影响网站排名的一些问题
  2. 解决win7下netbeans安装的错误问题
  3. Spring 3.2 @ControllerAdvice批注的异常处理
  4. 【Linux学习笔记】用nc实现两台主机间的文件传输(不需要输密码)
  5. 前端学习(1190):事件修饰符
  6. nginx php image,[Docker]应该把 nginx 和 PHP 放在一个 image 里还是分开?
  7. 新款iPhone SE是改良版iPhone 8无误了,部分零件可互换
  8. 三极管的经典模型——两个二极管连接和三极管人(transistor man)
  9. kvm+libvirt虚拟机快照浅析[转]
  10. 基于SSM+Bootstrap+MYSQL演唱会网上订票系统
  11. threejs特效:边缘暗角shader
  12. 如何下载历史版本的App Store中的APP
  13. Vue3 + Vite 使用 Less 配置
  14. hdmi线和vga线哪个好?用HDMI线和VGA线,显示屏画质到底相差多大?
  15. 网络分层和Http协议原理
  16. notepad++打开bin文件
  17. Linux常用命令-详细1
  18. Unity3d制作简单拼图游戏
  19. html5峰会2015,2015 iWeb峰会与第四届HTML5峰会  7月19日上海站重磅开幕
  20. Virual Studio 2022 C++ CLR 中 模拟 Android Studo,Eclipse 的 LogCat

热门文章

  1. vue.js关于循环出来多个input并让每个input后的修改按钮控制其值
  2. 多厂商云成本管理窍门
  3. newusers和chpasswd的用法
  4. Weblogic 9.2 for Linux AS4u5安装配置
  5. osm数据下载 python_用Python编写小工具下载OSM路网数据
  6. java web 基础框架搭建_JavaWeb之搭建自己的MVC框架(一)
  7. php绕过验证码注册,验证码被绕过的处理方法_PHP教程
  8. 小石坝第一次月赛:A
  9. C程序设计案例(牛顿迭代法求高次方程的根)
  10. 1074 Reversing Linked List (25 分)