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 ...
随机推荐
- Simple colum formatting in Yii 2 GridView
A very important widget in the business apps development is the GridView control. In this post I wil ...
- jQuery -- is() 方法
定义和用法: 根据选择器.DOM元素或 jQuery 对象来检测匹配元素集合,如果其中至少有一个元素符合这个给定的表达式就返回true.如果没有元素符合,或者表达式无效,都返回'false'. ''' ...
- Oracle nvl(),nvl2()函数介绍
NVL函数 Oracle/PLSQL中的一个函数. 格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值, ...
- 脱离 Spring 实现复杂嵌套事务,之一(必要的概念)
事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为, 它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播: 表1事务传播行为类型 事务 ...
- VisualStudio基本使用(1)-显示行号
"工具"-"选项"-"文本编辑器"-"C/C++"-"常规",勾选"行号"复选框 ...
- C语言strdup函数
static RD_INLINE RD_UNUSED char *rd_strdup(const char *s) { #ifndef _MSC_VER char *n = strdup(s); #e ...
- Win8/Win10无法打开这个应用 内置管理员账户
现在装win10系统的同伴越来越多了,相比于win7,win10在某些设置方面也有些变化,比如我们在使用win8或者win10时,会碰到如图所示的对话框: Windows10/Windows8无法使用 ...
- 几乎所有的html + css 内容的编写, 都可以通过emmet来写
在今后的html编写中, 强迫 / 必须 使用 emmet来编写html代码了 !!!! 只使用zen coding, 只使用emmet 来编写, 再也不用以前的那种移动过去移动过来的 写法: 原始的 ...
- javascript 数据类型转换
javascrīpt 类型转换函数 在Javascrīpt中,Double类型和Int类型都是看作为Number对象. 1.Number转成String number.toString() Strin ...
- java web
1,当访问完一个网页的时候,浏览器会有缓存,当你再次输入原来的网站是会有从远端传来的网页缓存,所以在测试的时候要清除缓存才行