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().
Here is an 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.
- class PeekingIterator implements Iterator<Integer> {
- private Integer next; //cache the next peek
- private Iterator<Integer> iter;
- public PeekingIterator(Iterator<Integer> iterator) {
- // initialize any member here.
- iter = iterator;
- if (iter.hasNext()) {
- next = iter.next();
- }
- }
- // Returns the next element in the iteration without advancing the iterator.
- public Integer peek() {
- return next;
- }
- // hasNext() and next() should behave the same as in the Iterator interface.
- // Override them if needed.
- @Override
- public Integer next() {
- Integer ret = next;
- next = iter.hasNext() ? iter.next() : null;
- return ret;
- }
- @Override
- public boolean hasNext() {
- return next != null;
- }
- }
Peeking Iterator的更多相关文章
- LeetCode OJ:Peeking Iterator(peeking 迭代器)
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- [LeetCode] Peeking Iterator 顶端迭代器
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- LeetCode Peeking Iterator
原题链接在这里:https://leetcode.com/problems/peeking-iterator/ 题目: Given an Iterator class interface with m ...
- 284. Peeking Iterator
题目: Given an Iterator class interface with methods: next() and hasNext(), design and implement a Pee ...
- Peeking Iterator 解答
Question Given an Iterator class interface with methods: next() and hasNext(), design and implement ...
- 【LeetCode】284. Peeking Iterator
题目: Given an Iterator class interface with methods: next() and hasNext(), design and implement a Pee ...
- [Java]LeetCode284. 顶端迭代器 | Peeking Iterator
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- LeetCode——Peeking Iterator
Description: Given an Iterator class interface with methods: next() and hasNext(), design and implem ...
- LeetCode(282) Peeking Iterator
题目 Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peek ...
随机推荐
- 微信jssdk录音功能开发记录
原文地址:http://www.cnblogs.com/liujunyang/p/4962423.html
- 史上最全的maven pom.xml文件教程详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- Linux静态库生成指南
Linux静态库生成指南 Linux上的静态库,其实是目标文件的归档文件.在Linux上创建静态库的步骤如下: 写源文件,通过 gcc -c xxx.c 生成目标文件. 用 ar 归档目标文件,生 ...
- Swing杂记——Swing中引入Android的NinePatch技术,让Swing拥有Android的外观定制能力
[摘要] 本文诣在展示如何在Swing中引入 NinePatch技术(早期有文章里中文译作九格图,暂且这么叫吧^_^,但此术非传统移动手机上的功能布局——九格图哦). [准备篇] Q:何为 NineP ...
- JMS基本概念和模型
------------------------------------------------------------------------------------------- JMS是什么 J ...
- 基础知识系列☞C#中数组Array、ArrayList和List三者的区别
数组() #region 数组 //初始化方式_0:先声明再赋值 ]; weekDays_0[] = "Sun"; weekDays_0[] = "Mon"; ...
- HttpClient连接池的连接保持、超时和失效机制
HTTP是一种无连接的事务协议,底层使用的还是TCP,连接池复用的就是TCP连接,目的就是在一个TCP连接上进行多次的HTTP请求从而提高性能.每次HTTP请求结束的时候,HttpClient会判断连 ...
- Ubuntu 14 如何解压 .zip、.rar 文件?
.zip 和 .rar 是Windows下常用的压缩文件,在Ubuntu中如何解压? [解压.zip文件] Ubuntu中貌似已经安装了unzip软件,解压命令如下: unzip ./FileName ...
- git之install
一.window安装 1.下载路径 https://git-for-windows.github.io/ 2.如何在windows下安装GIT_百度经验 3.做完上面两部打开Git bash即可执行g ...
- 怎样用路由器共享需要网页认证的wifi
设置步骤:第一步:登录管理界面 1.连接电脑使用单机能上网的电脑,通过网线连接到路由器的LAN口.2.登录管理界面打开电脑的浏览器,清空地址栏后,输入路由器的管理地址(以路由器底部标贴标识的管理地址为 ...