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 ...
随机推荐
- db连接池
目前常用的连接池有: DBCP:org.apache.commons.dbcp.BasicDataSource dataSource: 要连接的 datasource (通常我们不会定义在 serve ...
- failed parsing overlays.
clearn + rebuild + 重新运行: 删掉模拟器进程 + 重新运行:
- 第一行Kotlin系列(二)Intent隐式显式跳转及向下传值
1.Intent显式跳转页面 val button5 = findViewById<Button>(R.id.mButton5) button5.setOnClickListener { ...
- docker磁盘空间不足解决办法
docker磁盘空间不足解决办法 导入docker镜像时,错误提示:磁盘空间不足. 1.查看docker镜像存放目录空间大小 du -hs /var/lib/docker/ 2.停止docker服务. ...
- 蓝桥杯备战(一)3n+1问题
[问题描述] 考虑如下的序列生成算法:从整数 n 开始,如果 n 是偶数,把它除以 2:如果 n 是奇数,把它乘 3 加1.用新得到的值重复上述步骤,直到 n = 1 时停止.例如,n = 22 时该 ...
- IP协议及其它的小弟 ,我保证没人会看的
IP协议及其它的小弟 IP协议:127.0.0.1就一个32位的标识符.实际上: 类似这样的: 精神小伙慢慢看吧.我赌一包辣条你是不会认真看完的. IP协议的构成 地址解析协议 ARP 前面的叙述中我 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 数据访问和代码优先
上一篇文章(https://www.cnblogs.com/meowv/p/12909558.html)完善了项目中的代码,接入了Swagger.本篇主要使用Entity Framework Core ...
- 最短路径——floyd算法代码(c语言)
最短路径问题 昨天自己试了试写一下dijkstra的算法博客 dijkstra链接在这← 今天来更floyd算法,感觉非常简单果然暴力才是解决一切的王道 一.总体思想 floyd算法就是每一次从邻接矩 ...
- APP测试知识中的monkey测试
了解了logging模块的应用 1)两种方式,1:日志级别函数 2:日志级别的四大组件来实现日志功能(日志流处理) 2)日志流处理日志logging四大组件:logger(日志器) ...
- httpd+tomcat 均衡负载
接前面的文章http://www.cnblogs.com/gqdw/p/3785812.html workers.properties worker.list=controller#worker1 w ...