【leetcode】341. Flatten Nested List Iterator
题目如下:
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:
- Input: [[1,1],2,[1,1]]
- Output: [1,1,2,1,1]
- Explanation: By calling next repeatedly until hasNext returns false,
- the order of elements returned by next should be:
[1,1,2,1,1]
.Example 2:
- Input: [1,[4,[6]]]
- Output: [1,4,6]
- Explanation: By calling next repeatedly until hasNext returns false,
- the order of elements returned by next should be:
[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(object):
- # def isInteger(self):
- # """
- # @return True if this NestedInteger holds a single integer, rather than a nested list.
- # :rtype bool
- # """
- #
- # def getInteger(self):
- # """
- # @return the single integer that this NestedInteger holds, if it holds a single integer
- # Return None if this NestedInteger holds a nested list
- # :rtype int
- # """
- #
- # def getList(self):
- # """
- # @return the nested list that this NestedInteger holds, if it holds a nested list
- # Return None if this NestedInteger holds a single integer
- # :rtype List[NestedInteger]
- # """
- class NestedIterator(object):
- def __init__(self, nestedList):
- """
- Initialize your data structure here.
- :type nestedList: List[NestedInteger]
- """
- self.val = []
- def recursive(nestedList):
- for item in nestedList:
- if item.isInteger():
- self.val.append(item.getInteger())
- else:
- recursive(item.getList())
- recursive(nestedList)
- def next(self):
- """
- :rtype: int
- """
- return self.val.pop(0)
- def hasNext(self):
- """
- :rtype: bool
- """
- return 0 != len(self.val)
- # Your NestedIterator object will be instantiated and called as such:
- # i, v = NestedIterator(nestedList), []
- # while i.hasNext(): v.append(i.next())
【leetcode】341. Flatten Nested List Iterator的更多相关文章
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)
[LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- [LeetCode] 341. Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [leetcode]341. Flatten Nested List Iterator展开嵌套列表的迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- LeetCode 341. Flatten Nested List Iterator
https://leetcode.com/problems/flatten-nested-list-iterator/
- 341. Flatten Nested List Iterator展开多层数组
[抄题]: Given a nested list of integers, implement an iterator to flatten it. Each element is either a ...
- 341. Flatten Nested List Iterator
List里可以有int或者List,然后里面的List里面可以再有List. 用Stack来做比较直观 Iterator无非是next()或者hasNext()这2个方程 一开始我想的是hasNext ...
- 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...
- 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...
随机推荐
- 【Linux开发】linux设备驱动归纳总结(三):1.字符型设备之设备申请
linux设备驱动归纳总结(三):1.字符型设备之设备申请 操作系统:Ubunru 10.04 实验平台:S3C2440 + linux2.6.29内核 注:在今后驱动程序的学习中经常需要查看内核源代 ...
- yum tenxun ntpdate 时间同步
centos7 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base ...
- C语言中typedef,条件编译,结构体的说明
目录 typedef (类型别名) 条件编译 条件编译在头文件包含中的应用 结构体 使用结构体定义新的结构体变量 结构体成员的引用与赋值 结构体指针及其引用 typedef (类型别名) typede ...
- aws 基于延迟策略配置dns故障切换
前提:由于国内访问首尔地区经常出现不稳定情况,现将请求从nginx(sz)转发到nginx(hk)再转发到首尔地区,在基于不改变nginx(seoul)的配置的前提下,引入aws的延迟策略,同时保证国 ...
- SQL注入之手工注入
手工注入 用的是墨者学院的靶场:传送门 涉及以下数据库: MySQL.Access.SqlServer(MSSQL).SQLite.MongoDB.Db2(IBM).PostgreSQL.Sybase ...
- 手把手教你用 Strace 诊断问题
早些年,如果你知道有个 strace 命令,就很牛了,而现在大家基本都知道 strace 了,如果你遇到性能问题求助别人,十有八九会建议你用 strace 挂上去看看,不过当你挂上去了,看着满屏翻滚的 ...
- spring配置文件拆分策略及方法
一.拆分策略 如果一个开发人员负责一个模块,我们采用公用配置(包括数据源.事务等)+每个系统模块一个单独配置文件(包括Dao.Service.Web控制器)的形式 如果是按照分层进行的分工,我们采用公 ...
- python computer info look
计算机信息查看-. import platform import platform def TestPlatform(): print("---------SYSTEM INFO------ ...
- 手写一个python迭代器
分析 我们都知道一个可迭代对象可以通过iter()可以返回一个迭代器. 如果想要一个对象称为可迭代对象,即可以使用for,那么必须实现__iter __()方法. 在一个类的实例对象想要变成迭代器,就 ...
- FullPage.js全屏插件文档及使用方法
简介 fullPage.js是一个基于jQuery的全屏滚动插件,它能够很方便.很轻松的制作出全屏网站 下载地址 下载地址 相关示例:基于fullpage.js实现的360全屏滑动效果 支持功能 支持 ...