原题链接在这里: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. shell条件与循环

    一.if语句 if [expression] then elif[expression] then else fi 注 : expression前后要有空格:判断相等用 = 而不是 == : then ...

  2. 李洪强-C语言9-C语言的数据,变量和常量

    一.数据 图片文字等都是数据,在计算机中以0和1存储. (一)分类 数据分为静态数据和动态数据. ①. 静态数据:一些永久性的的数据,一般存储在硬盘中,只要硬盘没坏数据都是存在的.一般以文件的形式存储 ...

  3. Qt 程序和窗口添加图标

    Qt项目在打包发布之后都需要有个个性的程序图标和窗口图标,这样会使程序更加美观大方,下面我们分别来看如何给程序和窗口分别添加图标.我们需要两种格式的图片,一种是.ico的,用来给程序添加图标,一种是. ...

  4. python中的循环

    >>> x = 100 >>> y = 10>>> x < y and x or y10>>> x if x > y ...

  5. java---相亲练习

    public class wu{ public static void main(String[] args){ //TODO 自动生成的方法存根 int a = 0,b = 0,c = 0; int ...

  6. MemPool

    腾讯笔试题,设计内存池,alloc和free都是O(1). 和LRUCache类似,这里用了一个list表示可用的空间,用一个map来记录这块内存是否已分配,这样free的时候才可能O(1). cla ...

  7. CSV to XLSX (专用)

    $csvFile = "F:\ACL\HZ ACL\ACL-APAC.CSV" $path = "F:\ACL\HZ ACL\ACL-APAC.XLSX" $r ...

  8. PHP 二维数组根据相同的值进行合并

    例如有一个二维数组 $arr: $arr = array( array( 'review_id' =>102 , 'url'=>'a.jpg', ), array( 'review_id' ...

  9. mysql通过data目录恢复数据库

    mysql通过data目录恢复数据库 阅读:次   时间:2010-03-24 06:53:30   字体:[大 中 小]     重装系统后,MySQL服务没有了,但是数据库的文件还在,这个时候我想 ...

  10. 怎么样打印加密PDF文件

    自然是解密后再打印.解密的方法,在linux下执行: pdf2ps xxx.pdf ps2pdf xxx.ps 参考资料 http://www.cyberciti.biz/faq/removing-p ...