【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 ...
随机推荐
- eclipse中出现错误 Syntax error, insert "}" to complete Block
结果原因如下 划红线的地方多出来类似于空格的占位符,也许这样看不清楚. 我们来显示空格.制表符和回车键. Window->Preferences->General->Editors- ...
- eclipse -------导出war包
1.右键工程名--Export----- WAR file 2.输入war包名,选择导出路径,finish完成
- 登录进入Mysql数据库的几种方式
前提:连接进入mysql数据库 本机安装的myslq基础信息: host= "localhost", # 数据库主机地址:127.0.0.1 port=3306, # 端口号 us ...
- Nexus2 yum插件RCE漏洞(CVE-2019-5475)
1.漏洞简介: 安全研究员Christian August Holm Hansen在Nexus Repository Manager 2.X中披露了一个远程命令执行漏洞. 该漏洞默认具有部署权限帐户. ...
- JCC指令
0.JMP1.JE, JZ 结果为零则跳转(相等时跳转) ZF=12.JNE, JNZ 结果不为零则跳转(不相等时跳转) ZF=03.JS 结果为负则跳转 SF=14.JNS 结果为非负则 ...
- 查找担保圈-step2-拆分成员表函数
USE [test] GO /****** Object: UserDefinedFunction [dbo].[tf_split_char] Script Date: 2019/7/8 14:39: ...
- HDU-5238 Calculator
题目描述 给定一个关于 \(x\) 的表达式,形如下例:\(×4+2^3+8×6\) 按如下方法计算:\((((x×4)+2)^3+8)×6\) 运算符只有 加号,乘号,幂运算三种,给定的式子中有 \ ...
- 使用vs工具查看dll依赖(也可查看pyc文件的依赖)
vs工具中有个工具叫dumpin.exe,可以用来查看exe文件.dll文件.pyc文件依赖于哪些dll,从而针对性地去检查具体缺失哪些文件(目前是在装TensorFlow时查看具体需要哪个版本的cu ...
- 树上选两点(使最短)树的直径+bfs
题意: 给你一颗树,让你放两个点,放在哪里的时候任意点到某个最近的消防站最远值最小. 思路: 树的直径类题目. 首先我们想两个点会把整棵树分成两个团,所以肯定会在树的某个链上切开. 而且要切一定切在树 ...
- Python和其他编程语言
Python和其他编程语言 一.Python介绍 Python的创始人为吉多·范罗苏姆(Guido van Rossum),如下图,少数几个不秃头的语言创始人.1989年的圣诞节期间,Guido为了打 ...