【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. asp.net core 实现支持自定义 Content-Type

    asp.net core 实现支持自定义 Content-Type Intro 我们最近有一个原本是内网的服务要上公网,在公网上有一层 Cloudflare 作为网站的公网流量提供者,CloudFla ...

  2. python基础学习之元组和字典的功能方法

    什么是元组?(tuple) emmmmmm,这个没必要深究吧,就是一排'元素',一行 格式: a = (1,2,3,4,5,6,7,8,9)用小括号表示的,极为元组. 其有序,且不可更改,可以对比st ...

  3. Balanced Diet Gym - 102220B

    题目链接:https://vjudge.net/problem/Gym-102220B 题意:每组数据 给了 N和M表示有M种类型的糖果,这些糖果一共N个.接下了是 M 组数据,表示如果你选第 i 中 ...

  4. 找单词 HDU - 2082(普通母函数)

    题目链接:https://vjudge.net/problem/HDU-2082 题意:中文题. 思路:构造普通母函数求解. 母函数: 1 #include<time.h> 2 #incl ...

  5. DNA序列(JAVA语言)

    package 第三章习题; /*  * 输入m个长度均为n的DNA序列,求一个DNA序列,到所有序列的总Hamming距离尽量小.  * 两个等长字符串的Hamming距离等于字符不同的位置个数, ...

  6. FutureTask核心源码分析

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

  7. 使用C# (.NET Core) 实现观察者模式 (Observer Pattern) 并介绍 delegate 和 event

    观察者模式 这里面综合了几本书的资料. 需求 有这么个项目: 需求是这样的: 一个气象站, 有三个传感器(温度, 湿度, 气压), 有一个WeatherData对象, 它能从气象站获得这三个数据. 还 ...

  8. Android Studio 之 EditText

    EditText 简介 •简介 EditText是一个非常重要的组件,可以说它是用户和Android应用进行数据传输窗户: 有了它就等于有了一扇和Android应用传输的门,通过它用户可以把数据传给A ...

  9. 【Django】有关多用户管理的一点小经验分享

    前言 最近,笔者因为需要开发一个系统作为毕设的展示,因此就产生了有关多用户管理的问题.在这里我把自己的需求重新阐明一下:能够通过Django自带的用户管理框架,实现多用户的管理,例如登录.登出.ses ...

  10. 生产中常用的获取IP地址方法的总结

    从ifconfig命令的结果中筛选出除了lo网卡之外的所有IPv4地址 centos7 (1)ifconfig | awk '/inet / && !($2 ~ /^127/){pri ...