LeetCode Peeking Iterator
原题链接在这里:https://leetcode.com/problems/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.
题解:
设计一个是否被peek过得flag, isPeeked with initialization as false.
同时保存peek过得值peekVal. 若果isPeeked == true 已经peek过,next()直接返回peekVal, hasNext()直接看isPeek为true 就返回 true.
Time Complexity: peek, O(1). next, O(1). hasNext, O(1).
Space: O(1).
AC Java:
// Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
class PeekingIterator implements Iterator<Integer> {
Iterator<Integer> it;
boolean isPeeked;
int peekVal; public PeekingIterator(Iterator<Integer> iterator) {
// initialize any member here.
this.it = iterator;
isPeeked = false;
} // Returns the next element in the iteration without advancing the iterator.
public Integer peek() {
if(!isPeeked){
isPeeked = true;
if(it.hasNext()){
peekVal = it.next();
}else{
throw new IllegalStateException("iterator doesn't has next value.");
}
}
return peekVal;
} // hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
@Override
public Integer next() {
if(isPeeked){
isPeeked = false;
return peekVal;
}
return it.next();
} @Override
public boolean hasNext() {
return isPeeked || it.hasNext();
}
}
LeetCode Peeking Iterator的更多相关文章
- [LeetCode] 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 OJ:Peeking Iterator(peeking 迭代器)
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】284. Peeking Iterator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/peeking-i ...
- 【LeetCode】284. Peeking Iterator
题目: Given an Iterator class interface with methods: next() and hasNext(), design and implement a Pee ...
- LeetCode(282) Peeking Iterator
题目 Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peek ...
- [LeetCode] Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- LeetCode Zigzag Iterator
原题链接在这里:https://leetcode.com/problems/zigzag-iterator/ 题目: Given two 1d vectors, implement an iterat ...
随机推荐
- C#获取机器码(转)
/// <summary> /// 机器码 /// </summary> public class MachineCode { /// ...
- oracle 存储过程 基础
差不多一年没写过存储过程,最近要写,发现基本忘了,google一番之后,觉得很有必要把基础的东西写下来备忘. 语句块定义: decalre -- 变量声明 var1 ); -- 仅声明 var2 ) ...
- PixelFormat 像素格式
enum PixelFormat Pixel formats available for Format7 modes. Enumerator: PIXEL_FORMAT_MONO8 8 bits o ...
- [转] - C++程序启动过程
先说编译.链接过程1.预编译展开宏2.为每一个.cxx源文件编译一个目标文件3.编译器合成这些目标文件成一个库文件,同时解析可以找到的符号引用4.连接器把目标的库文件和所需要的引用的静.动态链接库进行 ...
- myeclipse10 将一个java工程合并到web工程
参考:zhaoshijie http://zhaoshijie.iteye.com/blog/ baidu: http://zhidao.baidu.com/link?url=WXr-EgI0OyUs ...
- cms 二级域名修改信息
\CMS\Collect\PageRes.cs _content = _content.Replace(r.orgurl, newurl); _content = _content.Replace(r ...
- Ubuntu 14.04 安装 VirtualBox
参考: ubuntu14.04,安装VirtualBox 5.0(虚拟机软件)! 由于Vagrant工具的需要,安装了一下VirtualBox. 使用参考链接的法一居然在软件中心里面报错,我想可能是没 ...
- 页面静态化2 --- 使用PHP缓存机制来完成页面静态化(上)(ob_flush和flush函数区别用法)
我们可以使用PHP自带的缓存机制来完成页面静态化,但在这里,需要说明一点,仅靠PHP缓存机制并不能完美的解决页面静态化,往往需要和其他页面静态技术(通常是伪静态技术)结合使用 例子: 当访问一个页面时 ...
- 解决mysql数据库连接问题
设置mysql远程连接root权限 在远程连接mysql的时候应该都碰到过,root用户无法远程连接mysql,只可以本地连,对外拒绝连接.需要建立一个允许远程登录的数据库帐户,这样才可以进行在远程操 ...
- Centos Apache安装eAccelerator
yum install php-eaccelerator