原题链接在这里: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的更多相关文章

  1. [LeetCode] Peeking Iterator 顶端迭代器

    Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...

  2. LeetCode——Peeking Iterator

    Description: Given an Iterator class interface with methods: next() and hasNext(), design and implem ...

  3. LeetCode OJ:Peeking Iterator(peeking 迭代器)

    Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...

  4. [LeetCode] 284. Peeking Iterator 瞥一眼迭代器

    Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...

  5. 【LeetCode】284. Peeking Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/peeking-i ...

  6. 【LeetCode】284. Peeking Iterator

    题目: Given an Iterator class interface with methods: next() and hasNext(), design and implement a Pee ...

  7. LeetCode(282) Peeking Iterator

    题目 Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peek ...

  8. [LeetCode] Zigzag Iterator 之字形迭代器

    Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...

  9. LeetCode Zigzag Iterator

    原题链接在这里:https://leetcode.com/problems/zigzag-iterator/ 题目: Given two 1d vectors, implement an iterat ...

随机推荐

  1. 去掉Xcode源码末尾的空格

    去掉Xcode源码末尾的空格 在用 Xcode 开发的时候,很容易就在行末增加一些空格了.这些空格在上传到 review board 上后 , 就会被特别的颜色显示出来.因为一种好的编程风格是说 , ...

  2. Scala中Zip相关的函数

    在Scala中存在好几个Zip相关的函数,比如zip,zipAll,zipped 以及zipWithIndex等等.我们在代码中也经常看到这样的函数,这篇文章主要介绍一下这些函数的区别以及使用. 1. ...

  3. javascript获取随机rgb颜色和十六进制颜色的方法

    <div id="console">在线交易平台的成功秘诀:从 Ebay 到 Yelp 到 Uber</div> <script type=" ...

  4. checking在浏览器为应用缓存查找更新时触发

    离线的Web应用,就是在设备不能上网的时候还能运行应用.html5把离线应用作为重点,主要是开发人员的心愿.离线应用的开发的步骤有:首先应该知道设备是否能够上网;然后应该还能访问一定的资源(如图像.C ...

  5. 按月将Windows日志导出至CSV文件

    # 这个月的第一天 #..........................................到这里之前是取当年第一天 #(Get-Date 0).AddYears((Get-Date). ...

  6. Excel 2003 中如何用VBA 代码访问单元格里的值及操作单元格 - 唐诗宋词的专栏 - 博客频道 - CSDN.NET

    在Excel 中编写VBA 代码,最常做的事可能就是操作表单中单元格里的数据. 我这里总结一下如何从VBA 代码中操作单元格的数据. 在VBA 代码中操作单元格需要用到Range 对象,Range 是 ...

  7. 使用Powershell取出属于某些指定组的用户并导出为csv

    知识这东西就像雪球,越滚越大,今天看到了这篇自己1年多前写的博文,简直弱爆了.于是更新一下程序: 2016-6-15更新,短短几行代码,就拿到了组和组成员,其中还用到了递归,以处理组成员是组的情况: ...

  8. MONGODB 计算机服务

    安装完成后,在 BIN目录执行如下 E:\APMServ5.2.6\MongoDb\bin>mongod --logpath E:\APMServ5.2.6\MongoDb\logs\Mongo ...

  9. Java静态块学习

    静态块是类里面的构造器,对象有构造器那么类也有构造器,类里面的构造器叫做初始化方法.也就是new一个对象他会经过一个构造器.加载一个类,也有被初始化的一片代码,这个就称之为静态块.一个类里面可以有很多 ...

  10. ios-滚动导航条页面

    // ViewController.m #import "ViewController.h" #import "ScrollSliderView.h" @int ...