Leetcode 284.顶端迭代器
顶端迭代器
给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext()。设计并实现一个支持 peek() 操作的顶端迭代器 -- 其本质就是把原本应由 next() 方法返回的元素 peek() 出来。
示例:
假设迭代器被初始化为列表 [1,2,3]。
调用 next() 返回 1,得到列表中的第一个元素。
现在调用 peek() 返回 2,下一个元素。在此之后调用 next() 仍然返回 2。
最后一次调用 next() 返回 3,末尾元素。在此之后调用 hasNext() 应该返回 false。
进阶:你将如何拓展你的设计?使之变得通用化,从而适应所有的类型,而不只是整数型?
[思路]
设一个 peeked 的flag 同时 保存 peeked 过得值. 如果已经peeked过了, next()直接返回保存的值即可.
// Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
class PeekingIterator implements Iterator<Integer> {
Iterator<Integer> itr;
boolean peeked;
int peekVal; public PeekingIterator(Iterator<Integer> iterator) {
// initialize any member here.
this.itr = iterator;
} // Returns the next element in the iteration without advancing the iterator.
public Integer peek() {
if(peeked) {
return peekVal;
} else {
peeked = true;
peekVal = itr.next();
}
return peekVal;
} // hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
@Override
public Integer next() {
if(peeked) {
peeked = false;
return peekVal;
} else return itr.next();
} @Override
public boolean hasNext() {
return peeked || itr.hasNext();
}
}
Leetcode 284.顶端迭代器的更多相关文章
- Java实现 LeetCode 284 顶端迭代器
284. 顶端迭代器 给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext().设计并实现一个支持 peek() 操作的顶端迭代器 – 其本质就是把原本应由 next() 方法 ...
- [LeetCode] Peeking Iterator 顶端迭代器
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- [Java]LeetCode284. 顶端迭代器 | Peeking Iterator
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- [LeetCode] 284. Peeking Iterator 瞥一眼迭代器
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- LeetCode 5123. 字母组合迭代器 Iterator for Combination
地址 https://leetcode-cn.com/contest/biweekly-contest-15/problems/iterator-for-combination/ 题目描述请你设计一个 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- C#LeetCode刷题-设计
设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制 33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- iphone6,键盘收起,H5页面下面出现空白
- 517 Super Washing Machines 超级洗衣机
详见:https://leetcode.com/problems/super-washing-machines/description/ C++: class Solution { public: i ...
- 【转】log4j的日志
一.Log4j配置 第一步:加入log4j-1.2.8.jar到lib下. 第二步:在CLASSPATH下建立log4j.properties.内容如下: 放在src下的话就不用配置 否则得去web. ...
- Spring------自动化装配Bean(一)
一.创建 CompactDisc接口和SgetPeppers实现类 CompactDisc接口方法为播放.SgtPeppers实现CompactDisc接口. package soundsystem; ...
- Ionic之数据绑定ng-model
ionic 完美的融合下一代移动框架,ionic 基于Angular语法,支持 Angularjs 的特性.但是我在开发的时候,遇到了坑.因为之后用的就是angularjs,so 理所当然的以为代码应 ...
- Day01 计算机硬件基础
1.什么是编程语言? 编程语言是程序员与计算机沟通的介质. 2.什么是编程? 程序员利用某种编程语言的语法格式将自己脑子中想要让计算机做的事写到文件中. 所以说,变成的结果就是文件,文件的内容就是一堆 ...
- 网页尺寸scrollHeight/offsetHeight
scrollHeight和scrollWidth,获取网页内容高度和宽度. 一.针对IE.Opera: scrollHeight 是网页内容实际高度,可以小于 clientHeight. 二.针对NS ...
- 【学习笔记】深入理解js原型和闭包(15)——闭包
前面提到的上下文环境和作用域的知识,除了了解这些知识之外,还是理解闭包的基础. 至于“闭包”这个词的概念的文字描述,确实不好解释,我看过很多遍,但是现在还是记不住. 但是你只需要知道应用的两种情况即可 ...
- 5款好用的mysql客户端
1. EMS SQL Manager for MySQL 是一款高性能MySQL数据库服务器系统的管理和开发工具.它支持从MySQL 3.23到6.0的任一版本,并支持最新版本的MySQL的特点,包括 ...
- java8的lambda表达式,将List<DTO> 转为 List<DO>
将List<PhoneDTO>转为List<PhoneDO>,通过java8的lambda表达式来操作,比传统的for循环精简很多: /** * List<PhoneDT ...