原题链接在这里: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. css 框架——base.css,作用是重设浏览器默认样式和提供通用原子类。自己留存

    今天发下我自己的 css 框架——base.css,作用是重设浏览器默认样式和提供通用原子类. @charset "utf-8"; /*! * @名称:base.css * @功能 ...

  2. Graph database_neo4j 底层存储结构分析(6)

    3.6  Node 数据存储 neo4j 中, Node 的存储是由 NodeStore 和 ArrayPropertyStore 2中类型配合来完成的. node 的label 内容是存在Array ...

  3. C#引用Interop.SQLDMO.dll后的注意事项(转)

    C#引用sqldmo.dll的方法 找到 sqldmo.dll这个文件C:\Program Files\Microsoft SQL Server\80\Tools\Binn\sqldmo.dll用.N ...

  4. myeclipse 8.5最新注册码

    myeclipse 8.5最新注册码(过期时间到2016年) Subscriber:huazai          Subscription Code:uLR8ZC-855550-6156585630 ...

  5. 封装同步的UIActionSheet

    封装同步的UIActionSheet 发问题 做 iOS 开发的同学想必都用过 UIActionSheet.UIActionSheet 可以弹出一个选择列表,让用户选择列表中的某一项操作.使用 UIA ...

  6. JS。 问题类型:穷举,迭代。两个关键词:break和continue

    问题类型: 穷举:(在不知道什么情况下是我们需要的结果的时候只能够让它一个一个都给走一遍) 百鸡百钱:公鸡1钱,母鸡2钱,小鸡0.5钱. 思路: 公鸡买100只,母鸡,小鸡都是0只: 母鸡50只,公鸡 ...

  7. java语言中一些使用的小技巧(区别于c++)

    正在自学java中...想记录下java和c++在一些小的方面的不同点.(未完待续...) java中class的对象均是引用类型的,如果想把连个同类型的对象相关联起来,只要将一个赋值给另一个就可以了 ...

  8. [转]SEP 11.x 迁移

    Sepm 迁移/灾难恢复     条件:更换服务器主机/重装服务器系统,但保持主机ip地址不更改,主机名不更改.     备份步骤:     Login 服务器控制台,进入"管理员" ...

  9. 修改CSV中的某些值 -- 2

    C:\aaa.csv "IPAddress","FullDomainName","RequestedTargetGroupName" &qu ...

  10. MySql之JDBC环境

    JAVA连接MySql数据库 JDBC对JAVA程序员而言是一套API "========" SQL语法 JDBC相关知识 jar命令的使用 异常的捕获和处理(Exception- ...