【python】Leetcode每日一题-扁平化嵌套列表迭代器

【题目描述】

给你一个嵌套的整型列表。请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数。

列表中的每一项或者为一个整数,或者是另一个列表。其中列表的元素也可能是整数或是其他列表。

示例1:

输入: [[1,1],2,[1,1]]
输出: [1,1,2,1,1]
解释: 通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,1,2,1,1]。

示例2:

输入: [1,[4,[6]]]
输出: [1,4,6]
解释: 通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,4,6]。

【分析】

  • AC代码:

    # """
    # This is the interface that allows for creating nested lists.
    # You should not implement it, or speculate about its implementation
    # """
    #class NestedInteger:
    # def isInteger(self) -> bool:
    # """
    # @return True if this NestedInteger holds a single integer, rather than a nested list.
    # """
    #
    # def getInteger(self) -> int:
    # """
    # @return the single integer that this NestedInteger holds, if it holds a single integer
    # Return None if this NestedInteger holds a nested list
    # """
    #
    # def getList(self) -> [NestedInteger]:
    # """
    # @return the nested list that this NestedInteger holds, if it holds a nested list
    # Return None if this NestedInteger holds a single integer
    # """
    global nested
    nested = []
    class NestedIterator:
    def __init__(self, nestedList):
    for x in nestedList:
    if x.isInteger():
    nested.append(x.getInteger())
    else:
    NestedIterator(x.getList()) def next(self) -> int:
    return nested.pop(0) def hasNext(self) -> bool:
    if(len(nested) == 0):
    return False
    return True
  • 有兄弟是用正则表达式直接写的

  • 官方:

    • dfs

    • public class NestedIterator implements Iterator<Integer> {
      
          private Stack<NestedInteger> stack;
      public NestedIterator(List<NestedInteger> nestedList) { stack = new Stack<>();
      for (int i=nestedList.size()-1;i>=0;i--) { stack.push(nestedList.get(i));
      }
      } @Override
      public Integer next() { NestedInteger temp = stack.pop();
      return temp.getInteger();
      } @Override
      public boolean hasNext() { while (!stack.isEmpty()) { NestedInteger temp = stack.peek();
      if (temp.isInteger()) return true; stack.pop();
      for (int i=temp.getList().size()-1;i>=0;i--) { stack.push(temp.getList().get(i));
      }
      }
      return false;
      }
      }

【python】Leetcode每日一题-扁平化嵌套列表迭代器的更多相关文章

  1. Java实现 LeetCode 341 扁平化嵌套列表迭代器

    341. 扁平化嵌套列表迭代器 给你一个嵌套的整型列表.请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 列表中的每一项或者为一个整数,或者是另一个列表.其中列表的元素也可能是整数或是其他列 ...

  2. 【python】Leetcode每日一题-二叉搜索迭代器

    [python]Leetcode每日一题-二叉搜索迭代器 [题目描述] 实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器: BSTIterator(T ...

  3. 【python】Leetcode每日一题-扰乱字符串

    [python]Leetcode每日一题-扰乱字符串 [题目描述] 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行 ...

  4. 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素

    [python]Leetcode每日一题-寻找旋转排序数组中的最小元素 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...

  5. 【python】Leetcode每日一题-删除有序数组中的重复项

    [python]Leetcode每日一题-删除有序数组中的重复项 [题目描述] 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现一次 ,返回删除后数组的新长度. 不要 ...

  6. 【python】Leetcode每日一题-存在重复元素3

    [python]Leetcode每日一题-存在重复元素3 [题目描述] 给你一个整数数组 nums 和两个整数 k 和 t .请你判断是否存在 两个不同下标 i 和 j,使得 abs(nums[i] ...

  7. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  8. 【python】Leetcode每日一题-打家劫舍2

    [python]Leetcode每日一题-打家劫舍2 [题目描述] 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋 ...

  9. 【python】Leetcode每日一题-二叉搜索树节点最小距离

    [python]Leetcode每日一题-二叉搜索树节点最小距离 [题目描述] 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 . 示例1: 输入:root = [4 ...

随机推荐

  1. FreeBSD 将降低对 i386 架构的支持力度

    FreeBSD 开发团队宣布,从 FreeBSD 13.0 开始,对 i386 架构的支持级别将降级为 Tier 2,未来的 14.0 可能还将会在此基础上进一步降低对 i386 架构的支持.而对于 ...

  2. 双向链表及有关操作(C语言)

    #include <stdio.h> #include <stdlib.h> /** * 含头节点双向链表定义及有关操作 */ //操作函数用到的状态码 #define TRU ...

  3. Dart简明教程

    [前言]Dart语言是使用flutter框架开发时候必备的语言,flutter是一个跨平台的框架,一套代码就可以完美实现安卓和ios两个平台,适配也很不错,Dart语言很友好,和java很类似,学习成 ...

  4. matplotlib常规使用方法

    1,指定图片大小和像素 Python绘图问题:Matplotlib中指定图片大小和像素 2,绘图命令的基本架构及其属性设置 绘图与可视化 3,python基础语法(二)--- plt的一些函数使用 p ...

  5. Shell prompt(PS1) 与 Carriage Return(CR) 的关系?-- Shell十三问<第二问>

    Shell prompt(PS1) 与 Carriage Return(CR) 的关系?-- Shell十三问<第二问> 当你成功登录进一个文字界面之后,大部份情形下,你会在荧幕上看到一个 ...

  6. 敏捷史话(十三):我被 Facebook 解雇了——Kent Beck

    2011年,Kent Beck 加入了 Facebook .那时候的他已年过半百,几十年的经验让他自认为非常了解软件行业.在 Facebook 的新手训练营期间,Kent 开始意识到,Facebook ...

  7. [BFS]最优乘车

    最优乘车 题目描述 HH 城是一个旅游胜地,每年都有成千上万的人前来观光.为方便游客,巴士公司在各个旅游景点及宾馆,饭店等地都设置了巴士站并开通了一些单程巴上线路.每条单程巴士线路从某个巴士站出发,依 ...

  8. 解决跨域问题chrome浏览器插件

    https://www.crx4chrome.com/crx/53489/ 解决chrome浏览器跨域的问题

  9. SQL语句通过身份证号计算年龄

    SQL语句通过身份证号计算年龄 1.截取身份证号上的出生日期 身份证一般为18位数和15位数 18位数身份证的第7-10位数是出生年份,第11-14位数是出生月日,所以18位身份证的年龄计算如下 su ...

  10. 【笔记】《Redis设计与实现》chapter19 事务

    chapter19 事务 Redis通过MULTI.EXEC.WATCH等命令来实现事务功能 19.1 事务的实现 事务开始 redis> MULTI ok 通过切换客户端状态的flag属性的R ...