Peeking Iterator 解答
Question
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.
Solution
Key to the solution is to cache status and value ahead.
// 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 int current;
private int peekValue;
private boolean hasNext; public PeekingIterator(Iterator<Integer> iterator) {
// initialize any member here.
this.iterator = iterator;
hasNext = this.iterator.hasNext();
if (hasNext)
peekValue = this.iterator.next();
} // Returns the next element in the iteration without advancing the iterator.
public Integer peek() {
return peekValue;
} // hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
@Override
public Integer next() {
current = peekValue;
hasNext = iterator.hasNext();
// Note here we need check hasNext, otherwise it will report runtime error
if (hasNext)
peekValue = iterator.next();
return current;
} @Override
public boolean hasNext() {
return hasNext;
}
}
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 ...
- 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 ...
- Zigzag Iterator 解答
Question Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...
- Binary Search Tree Iterator 解答
Question Implement an iterator over a binary search tree (BST). Your iterator will be initialized wi ...
- 【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 ...
随机推荐
- VC++ 2013 开发windows窗体程序
开发工具版本:Visual Studio Express 2013 for Windows Desktop 1. 新建Visual C++下面的"Win32 Project" 2. ...
- 【HDU1272】小希的迷宫(并查集基础题)
仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #inc ...
- HTTPS证书生成原理和部署细节
看看下面,部分电信用户访问京东首页的时候,会看到右下角有一个浮动广告: 小白用户以为是京东有意放置的,细心的用户会发现,这个 iframe 一层嵌一层的恶心广告很明显是电信/中间人通过 DNS 劫持注 ...
- Bom和Dom编程以及js中prototype的详解
一.Bom编程: 1.事件练习: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- hdu1016Prime Ring Problem
就是说,给你一个数n, 要你把1到n都连在一起成环. 每一个数不可反复, 且相连的两个数的和要是素数. 把全部情况输出来. 我是用dfs暴力出来的. 首先把素数打表, 然后每次顺时针预測下一个数 ...
- mmc加工配套问题
题目如下,本题还有其它解.
- Linux下redis的安装及用法
1.下载源代码包redis-2.8.21.tar.gz,并将其上传到指定文件夹/urs/src,然后对其进行解压: [root@Slave1pc src]# tar -xvf redis-2.8.21 ...
- ASP.NET三层架构的分析
BLL 是业务逻辑层 Business Logic Layer DAL 是数据访问层 Data Access Layer ASP.NET的三层架构(DAL,BLL,UI ...
- Java IO6 :IO总结
字节流.字符流继承关系 前几篇文章讲解了字节流.字符流的使用,不过Java提供给用户的流类远不止此,限于篇幅原因,没办法一一讲解,而且也没有必要一一讲解,就像我在写博客的时候多次提到的,有问题的时候学 ...
- scala maven pom文件
老司机的spark maven pom文件 pom文件一: 4.0.0 <groupId>com.glsx</groupId> <artifactId>spark- ...