【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. 多租缓存实现方案 (Java)

    多租缓存实现方案 (Java) 缓存在系统中是不可少的,缓存的实现是一个从无到有的过程,最开始,单应用的,缓存都是应用内部的,Map基本就能满足,实现简单.但是当上了微服务之后,应用是多部署的,应用之 ...

  2. SpringMVC-06 Ajax

    SpringMVC-06 Ajax Ajax 1.简介 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 是一种在无 ...

  3. 自定义校验注解ConstraintValidator

    一 前言 系统执行业务逻辑之前,会对输入数据进行校验,检测数据是否有效合法的.所以我们可能会写大量的if else等判断逻辑,特别是在不同方法出现相同的数据时,校验的逻辑代码会反复出现,导致代码冗余, ...

  4. vue全家桶常用命名

    1,版本查看 node -vnpm -v2,修改NPM的缓存目录和全局目录路径 D盘node目录下创建两个目录,分别是node_cache和node_global,这是用来放安装过程的缓存文件以及最终 ...

  5. D8016 “/ZI”和“/Gy-”命令行选项不兼容

    老版本vs项目升级到vs2017后遇到编译报错: /ZI选项在: /Gy- 选项在: 修改'/ZI'选项为'无' 或者 '/Gy-' 修改为'/Gy'

  6. 攻防世界 reverse 进阶 easyre-153

    easyre-153 查壳: upx壳 脱壳: 1 int __cdecl main(int argc, const char **argv, const char **envp) 2 { 3 int ...

  7. FutureTask核心源码分析

    本文主要介绍FutureTask中的核心方法,如果有错误,欢迎大家指出! 首先我们看一下在java中FutureTask的组织关系 我们看一下FutureTask中关键的成员变量以及其构造方法 //表 ...

  8. CPython-对象/类型系统

    Python中一切皆对象,包括实例对象和类型对象,如整数.浮点数.字符串是实例对象,整数类型.浮点数类型.字符串类型是类型对象. # [Python]>>> n=10 >> ...

  9. 学《跟我一起写Makefile》笔记发博词

    目录 笔记发博词 参考 笔记发博词 本系列笔记主要记录学了<跟我一起写Makefile>后的一些笔记 由于<跟我一起写Makefile>已经写得很详细了,所以我只是提取其中重要 ...

  10. django+x-admin管理后台模板开发管理后台案例(设计部分)

    使用django+x-admin管理后台模板搭建管理后台 一.环境需求 1.django:3.1 2.python:3.7 3.x-admin:2.2 4.pycharm:2020.3.2 5.ubu ...