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
相关推荐
Keep_Trying_Go几秒前
基于无监督backbone无需训练的类别无关目标统计CountingDINO算法详解
人工智能·python·算法·多模态·目标统计
weixin_4331793330 分钟前
python - for循环,字符串,元组基础
开发语言·python
^哪来的&永远~35 分钟前
Python 轻量级 UI:EEG 与 fNIRS 预处理图形界面
python·可视化·功能连接·eeg·mne·fnirs·eeglab
AI大佬的小弟37 分钟前
Python基础(11):Python中函数参数的进阶模式详解
python·lambda函数·函数的参数解释·函数的参数进阶·位置参数·关键词参数·匿名函数与普通函数
智算菩萨1 小时前
Python可以做哪些小游戏——基于Python 3.13最新特性的游戏开发全指南(15万字超长文章,强烈建议收藏阅读)
python·pygame
智航GIS1 小时前
9.1 多线程入门
java·开发语言·python
nvd111 小时前
FastMCP 开发指南: 5分钟入门
人工智能·python
weixin_433179332 小时前
Python - word jumble游戏
开发语言·python
Iridescent11213 小时前
Iridescent:Day48
python
BBB努力学习程序设计3 小时前
Python迭代器与生成器:优雅的惰性计算艺术
python