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();
	}
}
STM32步进电机控制三大方案:GPIO/定时器/PWM深度对比 步进电机控制是嵌入式运动控制的基础技术,其核心在于精确生成脉冲(PUL)、方向(DIR)和使能(EN)三路数字信号。原理上,脉冲频率决定转速,脉冲数量决定位置,方向电平决定转向,而使能时序保障驱动器可靠启停。该技术具有高定位精度、开环稳定性与低成本优势,广泛应用于3D打印机、CNC设备、自动化产线等场景。在STM32平台实现时,需兼顾硬件隔离设计(如6N137光耦)、HAL库抽象封装及实时性约束。本文围绕GPIO模拟、定时器中断与PWM比较输出三种主流方法,系统剖析其时序机制、CPU开销、抗干扰能力与工业适 阅读详情

相关推荐

广义平均(Generalized Mean, GeM)

本位介绍了广义平均操作的定义和简要推导,帮助大家理解其含义。

qq_45802280的博客 4561

Peeking Iterator 顶端迭代器

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

qq_26410101的博客 490

蓝牙6.0的‘新雷达’:Channel Sounding信道探测,真能比UWB更香吗?

本文探讨了蓝牙6.0的Channel Sounding信道探测技术在室内定位领域的应用潜力。通过与UWB技术的对比分析,揭示了其在成本、功耗和兼容性方面的优势,特别是在智能门锁、物品追踪等场景中的商业价值。Channel Sounding技术不仅具备亚米级精度,还能通过现有蓝牙设备实现零边际成本部署,为物联网应用提供了新的解决方案。

weixin_29288313的博客 349

Peeking Iterator

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

u011860731的专栏 512

四足机器人关节电机FOC控制实战:从磁编码器校准到SVPWM调试避坑指南

本文详细解析了四足机器人关节电机FOC控制实战,涵盖磁编码器校准、电流环调试和SVPWM波形优化等关键环节。通过MIT Cheetah案例展示如何解决21倍电气误差问题,并提供相序自动校正、PI参数整定等实用代码片段,帮助工程师避开永磁同步电机(PMSM)控制中的常见陷阱。

weixin_29281915的博客 499

STM32G474实战:数字LLC电源控制程序从零到跑通(附完整代码解析)

本文详细介绍了基于STM32G474微控制器开发数字LLC电源控制程序的完整实战流程。从硬件平台搭建、CubeMX工程配置,到核心控制逻辑、双速率中断框架、数字PID实现,再到Burst模式、状态机等高级策略与异常处理,提供了从零到跑通的系统化指南与代码解析,助力开发者掌握高效数字电源的程序编写与调试技巧。

s1t2u3的博客 1254

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

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

一直学习永不止步 1157

lattice 黑盒子的生成和使用(Creating Your Own Black Box Modules)

很多客户,特别是外包项目,需要做黑盒子,防止他人看到源码,那么这就需要对相关的源码打包加封了。diamond help 说的也是模糊模糊的,这次给出实际验证过的流程。 1.先将要加封的模块间一个工程,建好工程,写好代码之后,模块名字叫做crosslink(这个使用黑盒子的时候要用),代码如下: module crosslink( output wire[3:0] led, input wire clk, input wire reset ); reg [29:0] cnt; reg [3:0] led1;

qq_39633876的博客 1495

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

【AirPlay】苹果认证MFI学习

配件认证是一个过程,通过这个过程,可验证其配件符合MFi技术规范,并通过苹果指定的任何认证测试。MFi门户中的产品计划 "认证 "页面将指导你完成特定配件所需的认证步骤,并指出何时完成所有要求。在完成所有MFi认证要求之前,你不得制造、分发或销售任何配件。在开始MFi认证过程之前,你必须测试你的配件,以确保它符合最新的MFi技术规范,通过产品计划 "测试计划 "上的所有适用测试,并与它将声称具有兼容性的每个苹果设备兼容。

天天的博客 2290

世界职业院校技能大赛—人工智能赛道项目逐字稿参考十二

世界职业院校技能大赛—人工智能赛道项目逐字稿参考十二

我是网络安全兼技能大赛工程师,专注网络安全技术学习与技能大赛实战!持续分享最新的技术文章,帮你少走弯路,一起在技术赛道上进步~ 186

Leetcode284. Peeking Iterator顶端迭代器

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

lMonster81 272

华为OD机试 - 文件存储系统的排序 - 深度优先搜索dfs(Java 双机位C卷 200分)

华为OD机试 双机位C卷题库疯狂收录中,刷题。

学Java,找哪吒 725

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

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

weixin_30851409的博客 134

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode //C - 284. Peeking Iterator

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

Made in Code 815

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

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

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

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

无名山丘,崛起成峰 1271

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的专栏 287

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

小榕流光的专栏 1290

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

[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 235

[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(支持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 1012

[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、付费专栏及课程。

余额充值
⌂ 换网址