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 阅读详情

Design an iterator that supports the peek operation on an existing iterator in addition to the hasNext and the next operations.

Implement the PeekingIterator class:

PeekingIterator(Iterator nums) Initializes the object with the given integer iterator iterator.
int next() Returns the next element in the array and moves the pointer to the next element.
boolean hasNext() Returns true if there are still elements in the array.
int peek() Returns the next element in the array without moving the pointer.
Note: Each language may have a different implementation of the constructor and Iterator, but they all support the int next() and boolean hasNext() functions.

Example 1:

Input
[“PeekingIterator”, “next”, “peek”, “next”, “next”, “hasNext”]
[[[1, 2, 3]], [], [], [], [], []]
Output
[null, 1, 2, 2, 3, false]

Explanation
PeekingIterator peekingIterator = new PeekingIterator([1, 2, 3]); // [1,2,3]
peekingIterator.next(); // return 1, the pointer moves to the next element [1,2,3].
peekingIterator.peek(); // return 2, the pointer does not move [1,2,3].
peekingIterator.next(); // return 2, the pointer moves to the next element [1,2,3]
peekingIterator.next(); // return 3, the pointer moves to the next element [1,2,3]
peekingIterator.hasNext(); // return False

Constraints:

1 <= nums.length <= 1000
1 <= nums[i] <= 1000
All the calls to next and peek are valid.
At most 1000 calls will be made to next, hasNext, and peek.

模拟iterator,peek是检索当前iterator指向的元素,next是完成了peek的功能之后再把指针指向下一个元素。

思路:
先找个list把元素都装起来,再利用下标。
因为arrayList在add元素时,扩容会增加计算开销,所以要提前指定容量。

因为题目中说next和peek都是有效的,所以直接调用即可。

class PeekingIterator implements Iterator<Integer> {
    List<Integer> list = new ArrayList<>(1001);
    int id = 0;
    
	public PeekingIterator(Iterator<Integer> iterator) {
	    // initialize any member here.
        while(iterator.hasNext()) {
            list.add(iterator.next());
        }
	}
	
    // Returns the next element in the iteration without advancing the iterator.
	public Integer peek() {        
        return list.get(id);
	}
	
	// hasNext() and next() should behave the same as in the Iterator interface.
	// Override them if needed.
	@Override
	public Integer next() {
	    return list.get(id ++);
	}
	
	@Override
	public boolean hasNext() {
	    return id < list.size();
	}
}
维纳滤波和卡尔曼滤波 本文的主要内容是维纳滤波和卡尔曼滤波的介绍,包含匹配滤波器、维纳滤波器、维纳预测、卡尔曼滤波等内容。 阅读详情

相关推荐

手机流畅的秘密:拆解LPDDR5X在骁龙8 Gen2平台上的电源与信号完整性设计

本文深入解析了LPDDR5X内存与骁龙8 Gen2处理器在旗舰手机中的协同设计,重点探讨了0.5V超低电压下的电源完整性设计以及8.5Gbps高速传输中的信号完整性挑战。通过详细的电源树架构、关键器件选型和布线策略分析,揭示了如何实现高性能与低功耗的完美平衡,为手机流畅体验提供硬件保障。

weixin_30235225的博客 483

Peeking Iterator 顶端迭代器

给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext()。设计并实现一个支持 peek() 操作的顶端迭代器 -- 其本质就是把原本应由 next() 方法返回的元素 peek() 出来。 示例: 假设迭代器被初始化为列表 [1,2,3]。调用 next() 返回 1,得到列表中的第一个元素。现在调用 peek()返回 2,下一个元素。在此之后调用 next() 仍然...

qq_26410101的博客 490

制作 img系统镜像的详细方法

      I.MX6开发板目前支持从TF卡和eMMC启动,启动时,由于各个不同的启动文件对路径有不同的要求,例如uboot文件要求位于启动设备的某个块,dtb文件要求在FAT分区上,文件系统要求在Linux分区上,基于此,我们目前采用将映像制作成img文件的方式,这样可以更简单完成对启动卡的制作。 关于如何制作img,方法如下:方法一  Windows下面利用Win32工具1.   准备我司提供...

armkits的博客 4万+

Peeking Iterator

题意: 原本iterator中没有peek()方法,即无法取顶元素,现在你给加一个peek方法,next,hasNext功能不变 分析: 原本iterator不能取顶层元素,只是因为其next()方法执行过后,指针即后移,下一次再取next()时,就是下一个元素了; 所以,这里可以给它添加一个变量current指向顶层元素;然后需要修给next方法,保持current指向顶层元素 //

u011860731的专栏 510

Apifox CLI + Claude Skills:将接口自动化测试融入ai

基于 Apifox CLI、Claude Code以及Claude Skills组合,实现自然语言触发接口自动化测试。

wwz_0012的博客 596

Vue Print Designer——一款让打印模板的设计变简单的可视化打印设计器

Vue Print Designer——一款让打印模板的设计变简单的可视化打印设计器

bjzhang75的博客 682

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

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

一直学习永不止步 1155

10.2 全栈 CRUD 工程结构搭建:Cursor 4 步初始化 + 3 层目录规范

揭示 Cursor 工程初始化中 4 个关键动作如何将 AI 上下文感知能力从 30% 提升至 85%,包括用 pnpm create 显式声明结构契约、通过 monorepo.config.json 设定 workspace 根以支持跨包类型引用、在 shared 包中固化共享类型与协议,以及后续

weixin_42545292的博客 67

284. Peeking Iterator

284. Peeking Iterator方法1:易错点 Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation – it essentially peek() at t...

wilzxu的博客 223

智能车图像处理0-图像处理整体框架思路

1.总体框架如上所示,具体代码请查看后续文章。2.因为参与竞赛时间是大二,所以在编程中有许多不规范的地方(如变量的命名),大家见谅。3.原创文章,请勿侵权=-=

taiyuezyh的博客 1万+

如何创建 SAP ABAP CDS View 的 Association

所以本文我们虽然学习了如何创建 SAP CDS View 的 association,知道了 association 是用来在 CDS View 里将两张数据库表关联起来,但根本不知道怎么使用这个 association. 而且从 association 的写法来看,似乎和本教程之前介绍 inner join,left join 这些数据库表的连接操作没什么区别,都是根据两张表的 carrid 和 connid 字段相同进行的连接。只看到了我们在 CDS View 源代码实现里,放出来的四个字段。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 902

Leetcode284. Peeking Iterator顶端迭代器

给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext()。设计并实现一个支持 peek() 操作的顶端迭代器 -- 其本质就是把原本应由 next() 方法返回的元素 peek() 出来。 示例: 假设迭代器被初始化为列表 [1,2,3]。 调用 next() 返回 1,得到列表中的第一个元素。 现在调用 peek() 返回 2,下一个元素。在此之后调用 next() ...

lMonster81 270

【ArcGIS风暴】Win10操作系统ArcGIS许可License无法启动的解决方法汇总(绝对成功!)

文章目录1. 关闭Windows Defender2. 关闭系统防火墙3. 删除迈克菲(McAfee)杀毒软件4. 在系统服务中启动 在安装ArcGIS软件时,最重要的一步是许可服务的正常启动,通常,由于各种各样的原因,ArcGIS许可管理器License Manager十有八九会无法启动,点N多次都没反应,如果许可服务不能如愿启动的话,即使Desktop安装成功,也是无法打开的。本文汇总了Win10操作系统之上ArcGIS安装过程中可能会导致服务无法启动的原因及解决办法。 ArcGIS安装参考教程:

智慧空间实验室 2万+

[Java]LeetCode284. 顶端迭代器 | Peeking Iterator

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...

weixin_30851409的博客 132

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

LeetCode //C - 284. Peeking Iterator

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

Made in Code 813

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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的博客 257

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

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 pee...

杨领well的专栏 285

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 解题报告

题目链接: 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

小榕流光的专栏 1288

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

西施豆腐渣 5080

[leetcode] 284. Peeking Iterator

Description 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 w...

Learning from the mistakes 233

[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 进阶的专栏 3289

LeetCode 284. Peeking Iterator(支持peek的迭代器)

原题网址: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

James Pan 1011

[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
上一篇: cpp unique_lock
下一篇: ubuntu opencv 安装
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值