Java实现 LeetCode 284 顶端迭代器
284. 顶端迭代器
给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext()。设计并实现一个支持 peek() 操作的顶端迭代器 – 其本质就是把原本应由 next() 方法返回的元素 peek() 出来。
示例:
假设迭代器被初始化为列表 [1,2,3]。
调用 next() 返回 1,得到列表中的第一个元素。
现在调用 peek() 返回 2,下一个元素。在此之后调用 next() 仍然返回 2。
最后一次调用 next() 返回 3,末尾元素。在此之后调用 hasNext() 应该返回 false。
进阶:你将如何拓展你的设计?使之变得通用化,从而适应所有的类型,而不只是整数型?
// Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
class PeekingIterator implements Iterator<Integer> {
private Iterator<Integer> iterator;
private Integer cache = null; // 第一次peek时, 缓存迭代的元素
public PeekingIterator(Iterator<Integer> iter) {
iterator = iter;
}
public Integer peek() {
if (cache == null)
cache = iterator.next();
return cache;
}
@Override
public Integer next() {
if (cache == null)
return iterator.next();
Integer temp = cache;
cache = null;
return temp;
}
@Override
public boolean hasNext() {
return cache != null || iterator.hasNext();
}
}
Java实现 LeetCode 284 顶端迭代器的更多相关文章
- Leetcode 284.顶端迭代器
顶端迭代器 给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext().设计并实现一个支持 peek() 操作的顶端迭代器 -- 其本质就是把原本应由 next() 方法返回的元 ...
- [Java]LeetCode284. 顶端迭代器 | Peeking Iterator
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 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
随机推荐
- Quartus II 中参数化模块库(LPM)的使用
Quartus II 的LPM库所在的目录是\altera\11.0\quartus\libraries\megafunctions 现以LPM_MULT为例进行演示: 思路:1.首先创建一个pro ...
- c#得出两个列表的交集
c#提供了Intersect来得到两个列表的交集,它是通过使用默认的相等比较器对值进行比较生成两个序列的交集,定义为: public static IEnumerable<TSource> ...
- SpringBoot + SpringCloud的爬坑之旅
1,application.yaml中配置没有生效问题解决 如果配置文件确认没有错误但是没有生效首先是要到编译目录去查看是否被编译过去了,如果没有,请先将项目clean在重启 但是idea启动项目时也 ...
- promise的理解和使用
1. Promise是什么 1.1 promise 的理解 1. 抽象表达: Promise 是 JS 中进行异步编程的新的解决方案(旧的是纯回调形式) 2. 具体表达: (1)从语法上说:Promi ...
- 大话Ansible Ad-Hoc命令
Ansible是一个系列文章,我会尽量以通俗易懂.诙谐幽默的总结方式给大家呈现这些枯燥的知识点,让学习变的有趣一些. Ansible系列博文直达链接:Ansible入门系列 前言 通过前面的文章,大家 ...
- 【雕爷学编程】Arduino动手做(50)---W25Q64存储模块
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践(动手试试)出真知的理念,以学习和交流为目的,这里准备 ...
- jupyter notebook 修改前端样式
目录 jupyter notebook主题 修改css和js 最终效果 jupyter notebook主题 作者的GitHub地址:https://github.com/dunovank/jupyt ...
- Go语言学习目录
第一章 Go环境搭建 1.1 Linux搭建Go环境 1.2 Mac搭建Go环境 1.3 Windows搭建Go环境 第二章 Go语言特性 2.1 Go特征 2.2 Go变量类型 2.3 Go内置函数 ...
- Spring 基于注解的配置 简介
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
- 初识Java以及JAVA开发环境搭建
目录 JAVA帝国的诞生 C&C++ JAVA JAVA特性和优势 JAVA三大版本 JDK.JRE.JVE JAVA开发环境搭建 JDK下载与安装.卸载 安装JDK 卸载JDK JDK目录介 ...