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.

 class PeekingIterator implements Iterator<Integer> {
private Integer next; //cache the next peek
private Iterator<Integer> iter; public PeekingIterator(Iterator<Integer> iterator) {
// initialize any member here.
iter = iterator;
if (iter.hasNext()) {
next = iter.next();
}
} // Returns the next element in the iteration without advancing the iterator.
public Integer peek() {
return next;
} // hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
@Override
public Integer next() {
Integer ret = next;
next = iter.hasNext() ? iter.next() : null;
return ret;
} @Override
public boolean hasNext() {
return next != null;
}
}

Peeking Iterator的更多相关文章

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

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

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

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

  3. LeetCode Peeking Iterator

    原题链接在这里:https://leetcode.com/problems/peeking-iterator/ 题目: Given an Iterator class interface with m ...

  4. 284. Peeking Iterator

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

  5. Peeking Iterator 解答

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

  6. 【LeetCode】284. Peeking Iterator

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

  7. [Java]LeetCode284. 顶端迭代器 | Peeking Iterator

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

  8. LeetCode——Peeking Iterator

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

  9. LeetCode(282) Peeking Iterator

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

随机推荐

  1. centos 7.0 编译安装mysql 5.6.22 再次总结 成功编译安装~ 越来越熟练了~

    查找php.ini文件所在位置 [root@localhost /]# find -name php.ini ./usr/etc/php/etc/php.ini mysql官网的安装说明http:// ...

  2. PHP数组处理函数的使用array_map(三)

    /*函数array_map()函数:多数组回调函数---将回调函数作用到给定数组的单元上 * 1.语法:array array_map ( callback callback, array arr1 ...

  3. 抓取网站数据不再是难事了,Fizzler(So Easy)全能搞定

    首先从标题说起,为啥说抓取网站数据不再难(其实抓取网站数据有一定难度),SO EASY!!!使用Fizzler全搞定,我相信大多数人或公司应该都有抓取别人网站数据的经历,比如说我们博客园每次发表完文章 ...

  4. C#List的排序和简单去重总结

    List集合在开发过程中很常见,经常我们要对该集合进行一系列操作,本文介绍如何将该集合内的元素进行排序,博主制作简单WinForm应用程序进行演示. 首先,我们来看一下c#泛型List提供的Sort方 ...

  5. [工具]json转类

    摘要 这周在园子看到一篇介绍JsonCSharpClassGenerator这个工具的文章,感觉挺实用的,在现在项目中json用的是最多的,所以在转换对应的类的时候,确实挺频繁,所以就研究了一下这个工 ...

  6. 【转】apache kafka监控系列-KafkaOffsetMonitor

    apache kafka监控系列-KafkaOffsetMonitor 时间 2014-05-27 18:15:01  CSDN博客 原文  http://blog.csdn.net/lizhitao ...

  7. mysql 简单练习

    1.查找全部学生的信息 [SQL]select * from student 受影响的行: 0 时间: 0.000s 2.查出成绩及格的所有人 [SQL]select * from student w ...

  8. 请问如何查看mysql 的端口号?

    mysql> show variables like 'port'; +---------------+-------+ | Variable_name | Value | +--------- ...

  9. MEAN组合框架搭建教程

    1,我们先走在官方github里面下载个包文件: git clone https://github.com/linnovate/mean.git  (是慢了点) 2,我把这个文件解压后文件名叫mean ...

  10. NUGet的诞生与使用

    本文引用地址:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx NuGet 使用 NuGet 管理项目库 Phil Haack 无论多么努力 ...