LeetCode: 284. Peeking Iterator

leetcode 284. Peeking Iterator | 284. 顶端迭代器(给 iterator 添加 peek 方法) 题目 https://leetcode.com/problems/peeking-iterator/ 题解 一句话解释题意:标准的 iter 接口不包含 peek 功能,本题希望你能够增加这个功能。 // Java Iterator interface reference: // https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html class PeekingIterator implements Iterator<In 阅读详情

LeetCode: 284. Peeking Iterator

题目描述

Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation – it essentially peek() at the element that will be returned by the next call to next().

Example:

Assume that the iterator is initialized to the beginning of the list: [1,2,3].

Call next() gets you 1, the first element in the list.
Now you call peek() and it returns 2, the next element. Calling next() after that still return 2.
You call next() the final time and it returns 3, the last element.
Calling hasNext() after that should return false.
Follow up: How would you extend your design to be generic and work with all types, not just integer?

解题思路

将当前迭代器记录的数据暂存起来,并让迭代器指向下一个元素。每次 peek() 时,返回暂存的元素。

AC 代码

// Below is the interface for Iterator, which is already defined for you.
// **DO NOT** modify the interface for Iterator.
class Iterator {
    struct Data;
	Data* data;
public:
	Iterator(const vector<int>& nums);
	Iterator(const Iterator& iter);
	virtual ~Iterator();
	// Returns the next element in the iteration.
	int next();
	// Returns true if the iteration has more elements.
	bool hasNext() const;
};


class PeekingIterator : public Iterator {
private:
    int m_curNum;
    bool m_hasNext;
public:
	PeekingIterator(const vector<int>& nums) : Iterator(nums) {
	    // Initialize any member here.
	    // **DO NOT** save a copy of nums and manipulate it directly.
	    // You should only use the Iterator interface methods.
        if(Iterator::hasNext())
        {
            m_curNum = Iterator::next();
            m_hasNext = true;
        }
        else
        {
            m_hasNext = false;
        }
	}

    // Returns the next element in the iteration without advancing the iterator.
	int peek() {
        return m_curNum;
	}

	// hasNext() and next() should behave the same as in the Iterator interface.
	// Override them if needed.
	int next() {
        int lastNum = m_curNum;
	    
        if(Iterator::hasNext())
        {
            m_curNum = Iterator::next();
            m_hasNext = true;
        }
        else
        {
            m_hasNext = false;
        }
        
        return lastNum;
	}

	bool hasNext() const {
	    return m_hasNext;
	}
};
SystemVue 链路预算、仿真分析步骤 使用SystemVue进行链路预算、频谱分析等,相对ADS更全面。 阅读详情

相关推荐

Ubuntu中交叉编译armadillo库

网上关于交叉编译armdillo库比较少,借鉴了一些但是在前几天编译时总是磕磕绊绊,于是写一个详细的编译过程。交叉编译armdillo库包含两个步骤:交叉编译依赖库和交叉编译armdillo库。

weixin_39813867的博客 1881

leetcode:284. 顶端迭代器

题目来源 leetcode 题目描述 题目解析 class Iterator { struct Data; Data* data; public: Iterator(const vector<int>& nums); Iterator(const Iterator& iter); // Returns the next element in the iteration. int next(); // Return

OceanStar的博客 326

安装Tailscale

如果返回 /usr/bin/tailscale 或类似路径,说明已安装但 sudo 可能找不到它。如果 which tailscale 没有输出,说明安装失败,重新执行安装步骤。Tailscale 的后台服务 tailscaled 必须运行。检查 tailscaled 服务是否运行。检查 tailscale 命令是否存在。

FairLikeSnow的博客 1501

LeetCode题练习与总结:窥视迭代器--284

本文详细介绍了如何设计一个支持peek操作的PeekingIterator类,包括实现步骤、时间复杂度分析、空间复杂度分析以及涉及的知识点,为理解和实现此类迭代器提供了全面指导。

一直学习永不止步 1156

CXL技术入门:从PCIe到高性能计算的桥梁(附最新2.0协议解析)

本文深入解析CXL技术如何作为PCIe的进化版,成为高性能计算的关键桥梁,特别聚焦CXL 2.0协议的关键升级。从技术架构、三大子协议到典型应用场景,全面介绍CXL在缓存一致性、内存共享等方面的优势,帮助开发者快速掌握这一前沿技术。

wdx012345的博客 739

Hierarchical-Localization 环境配置与依赖管理全攻略

本文提供了Hierarchical-Localization(hloc)视觉定位工具箱从零开始的环境配置与依赖管理全攻略。针对Windows、Ubuntu和macOS三大操作系统,详细解析了Python版本选择、PyTorch与CUDA版本匹配、pycolmap版本锁定等核心依赖的安装步骤,并汇总了权重文件格式、NumPy版本、路径配置等常见问题的排查与解决方案,旨在帮助用户高效搭建稳定的hloc开发环境。

weixin_29066781的博客 338

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

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

Mopes__的博客 605

告别第三方插件:官方Fused Library如何重塑Android库的合并与发布流程

本文详细介绍了Android官方推出的Fused Library如何替代第三方fat-aar插件,优化Android库的合并与发布流程。通过原生支持、智能依赖管理等优势,Fused Library解决了版本兼容性、依赖传递等核心痛点,提供了更稳定高效的开发体验。文章还提供了迁移实操指南和工程实践建议,帮助开发者顺利过渡到官方方案。

weixin_42561040的博客 267

LeetCode 284 窥探迭代器[迭代器 构造类] HERODING的LeetCode之路

解题思路: 本质是继承 Iterator 类用来实现一个新的 Iterator 只不过多了一个peek,那么就很好解决了,next() 函数和 hasNext() 函数直接返回原来的 Iterator 的函数, peek() 函数用一个指针指向当前的位置,然后返回该指针的next即可,代码如下: /* * Below is the interface for Iterator, which is already defined for you. * **DO NOT** modify the inte.

HERODING23的博客 174

Unity类银河恶魔城学习记录1-10 PlayerWallJump源代码 P37

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考

Homura_的博客 1386

Node-RED教程(一):Node-RED的介绍与安装

最新最全的Node-RED,node-red教程都在这里。 本篇介绍node-red的安装。使用docker 安装node-red

知无涯 3万+

284. 顶端迭代器

注意:每种语言可能有不同的构造函数和迭代器 Iterator,但均支持 int next() 和 boolean hasNext() 函数。请你在设计一个迭代器,在集成现有迭代器拥有的 hasNext 和 next 操作的基础上,还额外支持 peek 操作。// 返回 1 ,指针移动到下一个元素 [1,2,3]// 返回 2 ,指针移动到下一个元素 [1,2,3]// 返回 3 ,指针移动到下一个元素 [1,2,3]int next() 返回数组中的下一个元素,并将指针移动到下个元素处。...

u013243296的博客 270

OSG 核心详解

OpenSceneGraph (OSG) 的核心架构、场景图管理以及几何体的绘制

qq_45859163的博客 735

leetcode 284: Peeking Iterator

Peeking Iterator Total Accepted: 2156 Total Submissions: 6859 Difficulty: Medium Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peekin

西施豆腐渣 5083

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

LeetCode //C - 284. Peeking Iterator

【代码】LeetCode //C - 284. Peeking Iterator。

Made in Code 814

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

leetcode284 : Peeking Iterator

1、原题如下: iven an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation – it essentially peek() at the element that will b

l3368bcttqnqn的博客 473

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

[leetcode] 284. Peeking Iterator 解题报告

题目链接: https://leetcode.com/problems/peeking-iterator/ Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek()operati

小榕流光的专栏 1289

LeetCode——284. 窥探迭代器(Peeking Iterator)[中等]——分析及代码(Java、C++)

LeetCode——284. 窥探迭代器[Peeking Iterator][中等]——分析及代码[Java、C++]一、题目二、分析及代码1. 迭代器(1)思路(2)代码(Java)(3)结果(Java)(4)代码(C++)(5)结果(C++)三、其他 一、题目 请你设计一个迭代器,除了支持 hasNext 和 next 操作外,还支持 peek 操作。 实现 PeekingIterator 类: PeekingIterator(int[] nums) 使用指定整数数组 nums 初始化迭代器。 int

江南土豆的博客 565

【leetcode】284. Peeking Iterator

题目: Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation – it essentially peek() at the element that will be returned by the next call to next(). 思路: 把下一个元素值以及其存在状态提

zxc120389574的博客 196

LeetCode284——Peeking Iterator新解

Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be

代码菌的blog 2867

leetcode 284. Peeking Iterator

leetcode 284. Peeking Iterator https://leetcode.com/problems/peeking-iterator/ 看了Discuss理解了, 关键是: 在class里面 cache两个variable, next用来store下一个Integer,这样每次peek都只用看next,不用move forward noSuchElement用来store是...

DeveloperKangK的博客 258

Leetcode 284. Peeking Iterator

Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek()operation -- it essentially peek() at the element that will be r

无名山丘,崛起成峰 1270

LeetCode 284. Peeking Iterator

题目链接: https://leetcode.com/problems/peeking-iterator/ Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek()ope

ibingoogle的博客 325

leetcode 284 Peeking Iterator

题目大意: 继承一个Iterator类,实现对下一个int元素的预览功能,想办法保持迭代器的位置。C++中peek()函数: istream::peek()// Below is the interface for Iterator, which is already defined for you. // **DO NOT** modify the interface for Iterator....

天天烤代码 880

[LeetCode 284] Peeking Iterator

Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be re

coder 进阶的专栏 3290

[leetcode-284]Peeking Iterator(java)

问题描述: Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation – it essentially peek() at the element that will be

中华胡杨的专栏 953
上一篇: LeetCode: 264. Ugly Number II
下一篇: 内存布局与栈
IT技术小密圈
博客等级 码龄12年 1385粉丝 322原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值