Python | Leetcode Python题解之第284题窥视迭代器

题目:

题解:

python 复制代码
class PeekingIterator:
    def __init__(self, iterator):
        self.iterator = iterator
        self._next = iterator.next()
        self._hasNext = iterator.hasNext()

    def peek(self):
        return self._next

    def next(self):
        ret = self._next
        self._hasNext = self.iterator.hasNext()
        self._next = self.iterator.next() if self._hasNext else 0
        return ret

    def hasNext(self):
        return self._hasNext
相关推荐
鸽芷咕18 分钟前
【Python报错已解决】ModuleNotFoundError: No module named ‘tensorflow‘
python·机器学习·tensorflow·bug·neo4j
李元豪21 分钟前
python 自动化 win11 编程 实现 一键 启动多个软件,QQ浏览器,snipaste,pycharm软件
python·pycharm·自动化
痛&快乐着21 分钟前
python-在PyCharm中使用PyQt5
python·qt·pycharm
fydw_71528 分钟前
PyTorch 激活函数及非线性变换详解
人工智能·pytorch·python
Rverdoser29 分钟前
在 PyCharm 中配置 Anaconda 环境
ide·python·pycharm
IT小辉同学44 分钟前
用 Pygame 实现一个乒乓球游戏
python·游戏·pygame
虚假程序设计1 小时前
pythonnet python图像 C# .NET图像 互转
开发语言·人工智能·python·opencv·c#·.net
测试老哥2 小时前
功能测试干了三年,快要废了。。。
自动化测试·软件测试·python·功能测试·面试·职场和发展·压力测试
爱吃油淋鸡的莫何2 小时前
Conda新建python虚拟环境问题
开发语言·python·conda
闲人编程2 小时前
Python实现日志采集功能
开发语言·python·fluentd·filebeat·日志采集