【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 ...
随机推荐
- 简单的利用nginx部署前端项目
网上有很多教程写的一大堆东西,新手可能会有点看不懂,现在我写这篇文章是为了更好的帮助新手,如何将自己的前端项目部署到自己的服务器上. 首先我们必须要有一台自己的ubuntu服务器,如果没有可以去阿里云 ...
- CMMI将能力成熟度分为5个级别
CMMI将能力成熟度分为5个级别(初始级,已管理级,已定义级,量化管理级,优化级) . 初始级 此时软件过程是无序的,有时甚至是混乱的,对过程几乎没有定义,成功取决于个人努力.管理是反应式的. .可管 ...
- Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- windows 装mac
必备条件: 1.vmware虚拟机 2.给相应版本虚拟机打mac补丁 3.用securable检测CPU支持虚拟化设置 4.mac镜像文件 5.这时候还不能启动虚拟机,还需要在引导文件里面进行参数修改 ...
- 【0.2】【MySQL】常用监控指标及监控方法(转)
[MySQL]常用监控指标及监控方法 转自:https://www.cnblogs.com/wwcom123/p/10759494.html 对之前生产中使用过的MySQL数据库监控指标做个小结. ...
- Oracle表概念
对于初学者来说,对表的概念也有一定的认识.因为我们对数据库的操作,90%以上是对表的操作. 常见表的规则表(Regular table),严格意义上来说又叫 heap table(堆表),也就是我们最 ...
- 版本控制器之SVN(二)
安装重启以后,在菜单栏找到TortoiseSVN程序 启动以后 点击: 填写相应的信息: 可以看到项目的相关信息 选中仓库,右键 > Browse Repository 进入如下界面: 可以打开 ...
- leecode刷题(29)-- 二叉树的中序遍历
leecode刷题(29)-- 二叉树的中序遍历 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 思路 跟 ...
- Entity Framework Core Relationship的学习笔记
说明 此例筛选了感兴趣及常用部分 参考文献 https://docs.microsoft.com/en-us/ef/core/modeling/relationships One to Many Ma ...
- .Net高并发解决思路
转自: 本文如有不对之处,欢迎各位拍砖扶正.另源码在文章最下面,大家下载过后先还原一下nuget包,需要改一下redis的配置,rabbitmq的配置以及Ef的连接字符串.另外使用的是CodeFirs ...