【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. jdk 集合大家族之Map

    jdk 集合大家族之Map 前言: 之前章节复习了Collection接口相关,此次我们来一起回顾一下Map相关 .本文基于jdk1.8. 1. HashMap 1.1 概述 HashMap相对于Li ...

  2. Java标识符,关键字,保留字三者区分(主要是从官网摘抄)

    区分 标识符(Identifiers) An identifier is an unlimited-length sequence of Java letters and Java digits, t ...

  3. 手把手教你docker安装宝塔面板

    手把手教你docker安装宝塔面板(针对小白提一下:需要先安装docker,安装docker的教程请自行百度,非常简单的),docker安装宝塔面板教程如下: 一.拉取centos镜像(当然这里拉取别 ...

  4. P1422小玉家的电费(JAVA语言)

    package 顺序与分支; /* * 题目描述 夏天到了,各家各户的用电量都增加了许多,相应的电费也交的更多了. 小玉家今天收到了一份电费通知单.小玉看到上面写:据闽价电[2006]27号规定, 月 ...

  5. springboot集成swagger实战(基础版)

    1. 前言说明 本文主要介绍springboot整合swagger的全过程,从开始的swagger到Knife4j的进阶之路:Knife4j是swagger-bootstarp-ui的升级版,包括一些 ...

  6. [源码解析] 并行分布式框架 Celery 之架构 (2)

    [源码解析] 并行分布式框架 Celery 之架构 (2) 目录 [源码解析] 并行分布式框架 Celery 之架构 (2) 0x00 摘要 0x01 上文回顾 0x02 worker的思考 2.1 ...

  7. github文件快速下载

    目录 一,提升加载速度 二,提升下载速度 只是想快速下载文件的直接看第二部分. github加载速度慢究其原因还是伟大的墙的存在.我们需要赞美墙,但就算墙很伟大,问题还是要解决的. 有问题就解决问题, ...

  8. Android Studio 之 用 Drawable resource file 美化 Button 样式

    shape •新建 Drawable resource file 点击 app/src/main/res 找到 drawable 文件夹,右击->New->Drawable Resourc ...

  9. 【linux】驱动-7-平台设备驱动

    目录 前言 7. 平台设备驱动 7.1 平台总线 7.1.1 平台总线注册和匹配方式 7.1.2 源码分析 7.2 平台设备 7.2.1 platform_device 7.2.2 设备信息 7.2. ...

  10. Azure App Service(一)利用Azure DevOps Pipeline 构建镜像,部署应用程序

    一,引言 起因是前两天项目上做测试,需要我把写好的基于.NET 5 的 Web 测试程序作成 Docker 镜像.当我在本地验证完功能后,准备利用 Docker 构建应用程序镜像的时候,发现系统不支持 ...