【python】Leetcode每日一题-扁平化嵌套列表迭代器
【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每日一题-扁平化嵌套列表迭代器的更多相关文章
- Java实现 LeetCode 341 扁平化嵌套列表迭代器
341. 扁平化嵌套列表迭代器 给你一个嵌套的整型列表.请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 列表中的每一项或者为一个整数,或者是另一个列表.其中列表的元素也可能是整数或是其他列 ...
- 【python】Leetcode每日一题-二叉搜索迭代器
[python]Leetcode每日一题-二叉搜索迭代器 [题目描述] 实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器: BSTIterator(T ...
- 【python】Leetcode每日一题-扰乱字符串
[python]Leetcode每日一题-扰乱字符串 [题目描述] 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行 ...
- 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素
[python]Leetcode每日一题-寻找旋转排序数组中的最小元素 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...
- 【python】Leetcode每日一题-删除有序数组中的重复项
[python]Leetcode每日一题-删除有序数组中的重复项 [题目描述] 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现一次 ,返回删除后数组的新长度. 不要 ...
- 【python】Leetcode每日一题-存在重复元素3
[python]Leetcode每日一题-存在重复元素3 [题目描述] 给你一个整数数组 nums 和两个整数 k 和 t .请你判断是否存在 两个不同下标 i 和 j,使得 abs(nums[i] ...
- 【python】Leetcode每日一题-前缀树(Trie)
[python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
- 【python】Leetcode每日一题-打家劫舍2
[python]Leetcode每日一题-打家劫舍2 [题目描述] 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋 ...
- 【python】Leetcode每日一题-二叉搜索树节点最小距离
[python]Leetcode每日一题-二叉搜索树节点最小距离 [题目描述] 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 . 示例1: 输入:root = [4 ...
随机推荐
- java基础:数据类型拓展
public static void main(String[] args) { //单行注释 //输出hello,world! //System.out.println("hello,wo ...
- Pandas文件读取——Pandas.read_sql() 详解
目录 一.函数原型 二.常用参数说明 三.连接数据库方式--MySQL ①用sqlalchemy包构建数据库链接 ②用DBAPI构建数据库链接 ③将数据库敏感信息保存在文件中 一.函数原型 panda ...
- 使用C# (.NET Core) 实现观察者模式 (Observer Pattern) 并介绍 delegate 和 event
观察者模式 这里面综合了几本书的资料. 需求 有这么个项目: 需求是这样的: 一个气象站, 有三个传感器(温度, 湿度, 气压), 有一个WeatherData对象, 它能从气象站获得这三个数据. 还 ...
- Redis实战篇(二)基于Bitmap实现用户签到功能
很多应用上都有用户签到的功能,尤其是配合积分系统一起使用.现在有以下需求: 签到1天得1积分,连续签到2天得2积分,3天得3积分,3天以上均得3积分等. 如果连续签到中断,则重置计数,每月重置计数. ...
- C/C++ 中的算术及其陷阱
目录 概述 C/C++ 整数的阴暗角落 整型字面量 整型提升与寻常算术转换 算术溢出检测 位运算技巧 总结 参考 概述 无符号数和有符号数是通用的计算机概念,具体到编程语言上则各有各的不同,程序员是解 ...
- 关于误删除elasticSearch 索引,怎么能快速找回?
背景 之前公司小王在工作中清理elasticSearch 索引,不小心使用脚本清空了最近使用的重要索引,导致开发无法准确的进行定位生产问题,造成了很大困扰. 当时我们的生产环境中是这样配置日志系统的: ...
- [面试仓库]CSS面试题汇总--布局篇
一,盒模型 说到 CSS 布局这块的内容,首当其冲的就是我们的盒模型宽度计算问题,在开始我们的问题之前,我们首先要搞懂这些概念: 盒模型里面的内容(content): 也就是实实在在要展现的内容, ...
- Java基础 Java-IO流 深入浅出
建议阅读 重要性由高到低 Java基础-3 吃透Java IO:字节流.字符流.缓冲流 廖雪峰Java IO Java-IO流 JAVA设计模式初探之装饰者模式 为什么我觉得 Java 的 IO 很复 ...
- "Unmapped Spring configuration files found.Please configure Spring facet."解决办法
最近在学习使用IDEA工具,觉得与Eclipse相比,还是有很多的方便之处. 但是,当把自己的一个项目导入IDEA之后,Event Log提示"Unmapped Spring configu ...
- 《疯狂Kotlin讲义》读书笔记4——流程控制
流程控制 与Java类似,Kotlin同样提供了两种基本的流程控制结构:分支结构和循环结构. Kotlin提供了 if 和 when 两种分支语句,其中 when 语句可以代替Java的switch语 ...