题目如下:

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:

  1. Input: [[1,1],2,[1,1]]
  2. Output: [1,1,2,1,1]
  3. Explanation: By calling next repeatedly until hasNext returns false,
  4.   the order of elements returned by next should be: [1,1,2,1,1].

Example 2:

  1. Input: [1,[4,[6]]]
  2. Output: [1,4,6]
  3. Explanation: By calling next repeatedly until hasNext returns false,
  4.   the order of elements returned by next should be: [1,4,6].

解题思路:题目不难,递归解析即可。

代码如下:

  1. # """
  2. # This is the interface that allows for creating nested lists.
  3. # You should not implement it, or speculate about its implementation
  4. # """
  5. #class NestedInteger(object):
  6. # def isInteger(self):
  7. # """
  8. # @return True if this NestedInteger holds a single integer, rather than a nested list.
  9. # :rtype bool
  10. # """
  11. #
  12. # def getInteger(self):
  13. # """
  14. # @return the single integer that this NestedInteger holds, if it holds a single integer
  15. # Return None if this NestedInteger holds a nested list
  16. # :rtype int
  17. # """
  18. #
  19. # def getList(self):
  20. # """
  21. # @return the nested list that this NestedInteger holds, if it holds a nested list
  22. # Return None if this NestedInteger holds a single integer
  23. # :rtype List[NestedInteger]
  24. # """
  25.  
  26. class NestedIterator(object):
  27.  
  28. def __init__(self, nestedList):
  29. """
  30. Initialize your data structure here.
  31. :type nestedList: List[NestedInteger]
  32. """
  33. self.val = []
  34.  
  35. def recursive(nestedList):
  36. for item in nestedList:
  37. if item.isInteger():
  38. self.val.append(item.getInteger())
  39. else:
  40. recursive(item.getList())
  41.  
  42. recursive(nestedList)
  43.  
  44. def next(self):
  45. """
  46. :rtype: int
  47. """
  48. return self.val.pop(0)
  49.  
  50. def hasNext(self):
  51. """
  52. :rtype: bool
  53. """
  54. return 0 != len(self.val)
  55.  
  56. # Your NestedIterator object will be instantiated and called as such:
  57. # i, v = NestedIterator(nestedList), []
  58. # while i.hasNext(): v.append(i.next())

【leetcode】341. Flatten Nested List Iterator的更多相关文章

  1. 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...

  2. 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)

    [LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  3. [LeetCode] 341. Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  4. [leetcode]341. Flatten Nested List Iterator展开嵌套列表的迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  5. LeetCode 341. Flatten Nested List Iterator

    https://leetcode.com/problems/flatten-nested-list-iterator/

  6. 341. Flatten Nested List Iterator展开多层数组

    [抄题]: Given a nested list of integers, implement an iterator to flatten it. Each element is either a ...

  7. 341. Flatten Nested List Iterator

    List里可以有int或者List,然后里面的List里面可以再有List. 用Stack来做比较直观 Iterator无非是next()或者hasNext()这2个方程 一开始我想的是hasNext ...

  8. 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...

  9. 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...

随机推荐

  1. 【Linux开发】linux设备驱动归纳总结(三):1.字符型设备之设备申请

    linux设备驱动归纳总结(三):1.字符型设备之设备申请 操作系统:Ubunru 10.04 实验平台:S3C2440 + linux2.6.29内核 注:在今后驱动程序的学习中经常需要查看内核源代 ...

  2. yum tenxun ntpdate 时间同步

    centos7 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base ...

  3. C语言中typedef,条件编译,结构体的说明

    目录 typedef (类型别名) 条件编译 条件编译在头文件包含中的应用 结构体 使用结构体定义新的结构体变量 结构体成员的引用与赋值 结构体指针及其引用 typedef (类型别名) typede ...

  4. aws 基于延迟策略配置dns故障切换

    前提:由于国内访问首尔地区经常出现不稳定情况,现将请求从nginx(sz)转发到nginx(hk)再转发到首尔地区,在基于不改变nginx(seoul)的配置的前提下,引入aws的延迟策略,同时保证国 ...

  5. SQL注入之手工注入

    手工注入 用的是墨者学院的靶场:传送门 涉及以下数据库: MySQL.Access.SqlServer(MSSQL).SQLite.MongoDB.Db2(IBM).PostgreSQL.Sybase ...

  6. 手把手教你用 Strace 诊断问题

    早些年,如果你知道有个 strace 命令,就很牛了,而现在大家基本都知道 strace 了,如果你遇到性能问题求助别人,十有八九会建议你用 strace 挂上去看看,不过当你挂上去了,看着满屏翻滚的 ...

  7. spring配置文件拆分策略及方法

    一.拆分策略 如果一个开发人员负责一个模块,我们采用公用配置(包括数据源.事务等)+每个系统模块一个单独配置文件(包括Dao.Service.Web控制器)的形式 如果是按照分层进行的分工,我们采用公 ...

  8. python computer info look

    计算机信息查看-. import platform import platform def TestPlatform(): print("---------SYSTEM INFO------ ...

  9. 手写一个python迭代器

    分析 我们都知道一个可迭代对象可以通过iter()可以返回一个迭代器. 如果想要一个对象称为可迭代对象,即可以使用for,那么必须实现__iter __()方法. 在一个类的实例对象想要变成迭代器,就 ...

  10. FullPage.js全屏插件文档及使用方法

    简介 fullPage.js是一个基于jQuery的全屏滚动插件,它能够很方便.很轻松的制作出全屏网站 下载地址 下载地址 相关示例:基于fullpage.js实现的360全屏滑动效果 支持功能 支持 ...