341. 扁平化嵌套列表迭代器
2020-05-24
1.题目描述
扁平化嵌套列表迭代器
2.题解
使用递归即可
3.代码
/*** // 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) {int l=nestedList.size();for (int i=0;i<l;i++){if (nestedList[i].isInteger()){ // 如果是数字的话myque.push(nestedList[i].getInteger());}else{fun(nestedList[i].getList());}}}void fun(vector<NestedInteger> list){int l=list.size();for (int i=0;i<l;i++){if (list[i].isInteger()){ // 如果是数字的话myque.push(list[i].getInteger());}else{fun(list[i].getList());}}}int next() {if (!myque.empty()){int tmp=myque.front();myque.pop();return tmp;}return -1;}bool hasNext() {if (!myque.empty()) return true;return false;}queue<int> myque;
};/*** Your NestedIterator object will be instantiated and called as such:* NestedIterator i(nestedList);* while (i.hasNext()) cout << i.next();*/
341. 扁平化嵌套列表迭代器相关推荐
- leetcode 341. Flatten Nested List Iterator | 341. 扁平化嵌套列表迭代器(Java)
题目 https://leetcode.com/problems/flatten-nested-list-iterator/ 这标题,翻译的是人话吗?啥叫扁平化嵌套列表迭代器?.. 题解 比较像深度优 ...
- leetcode 341. 扁平化嵌套列表迭代器(dfs)
给你一个嵌套的整型列表.请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 列表中的每一项或者为一个整数,或者是另一个列表.其中列表的元素也可能是整数或是其他列表. 示例 1: 输入: [[1 ...
- LeetCode 341. 扁平化嵌套列表迭代器(双栈)
文章目录 1. 题目 2. 双栈解题 1. 题目 给定一个嵌套的整型列表.设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 列表中的项或者为一个整数,或者是另一个列表. 示例 1: 输入: [[ ...
- 力扣--扁平化嵌套列表迭代器
扁平化嵌套列表迭代器 文章目录 扁平化嵌套列表迭代器 一.题目描述 二.分析 方法一: 代码一: 方法二: 代码二: C++代码: 一.题目描述 /*** // This is the interfa ...
- 扁平化嵌套列表迭代器 [树的递归前序遍历 + 迭代前序遍历]
递归|迭代的前序遍历 前言 一.扁平化嵌套列表迭代器 二.前序遍历(无需访问非叶节点) 1.递归实现 2.栈模拟 3.惰性栈模拟(next时再入栈) 总结 参考文献 前言 问题转换:将需求确认,结合已 ...
- 每日一题:leetcode341.扁平化嵌套列表迭代器
题目描述 题目分析 这个题目自己大概花了一个小时,虽然是一遍AC,但是速度有点慢,太长时间不写代码导致自己对代码不太敏感,写起来慢腾腾的. 看到这个的想法就是,要用栈来保存列表的迭代器,这样将孩子列表 ...
- leetcode算法题--扁平化嵌套列表迭代器
题目链接:https://leetcode-cn.com/problems/flatten-nested-list-iterator/ 这个题目不难,就是像树的深度遍历,就是题目有点难理解. /*** ...
- Python花式编程:多层嵌套列表扁平化
封面图片:<Python程序设计基础(第2版)>,ISBN:9787302490562,董付国,清华大学出版社 图书详情: 好消息:智慧树网APP"知到"中搜索&quo ...
- Python使用递归对任意嵌套列表进行扁平化
首先补充一个地方,之前有个文章演示的是猜数游戏,原文链接为猜数游戏用Python应该这样写,代码中漏掉了一个break语句,也就是说,在猜对的时候输出语句print('Congratulations! ...
最新文章
- atitit.人脸识别的应用场景and使用最佳实践 java .net php
- websocket实现方式
- Java 之文件目录操作
- 在Android中处理屏幕布局变化
- habernet备份mysql_harbor 安装
- 数据分析中会常犯哪些错误,如何解决? 三
- 基于flask框架的高校舆情分析系统
- 论文笔记:Securing Data With Blockchain and AI
- java 验证码不显示,页面没有显示验证码解决办法
- Mac安装Linux虚拟机
- 在家佛弟子对待工作的态度——世俗八正道
- 关于泛型中包含级联的List转化为json数据的处理
- Shader学习之Cg语言一(Cg语言概述)
- 【ARM 嵌入式 C 入门及渐进 4-- Linux 位图 bitmap】
- 如果取消Windows Ink后ps画笔没有压感
- orb-slam2 从单目开始的简单学习(7):Optimizer
- 【JavaScript】Ajax
- ASCII码和Unicode字符集
- hdu6112 今夕何夕
- ANSYS使用APDL数组存储每一个荷载子步下的后处理参数值
热门文章
- 目标群体是什么意思_抖音代运营日常主要是做什么的?
- 轨迹相似性度量方法总结
- 如何Super Vectorizer从Mac 上的图像中删除多余的颜色区域?
- PS图片无损放大插件 Alien Skin Blow Up 3 for Mac
- 1282:最大子矩阵《信息学奥赛一本通》
- linux修改容器内的mysql端口_Linux系统下修改phpstudy集成环境中的MySQL端口号的步骤...
- 中文 lda数据预处理_英文文本挖掘预处理流程总结
- Linux笔记-查询进程,获取其运行时输入的参数
- Qt5.7+Opencv2.4.9人脸识别(三)人脸处理
- 汇编和可执行文件(Debug和Release)