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. OC-ARC

    一. 基本简介 ARC是自iOS 5之后增加的新特性,完全消除了手动管理内存的烦琐,编译器会自动在适当的地方插入适当的retain.release.autorelease语句.你不再需要担心内存管理, ...

  2. Yii2提交表单提示无法验证

    yii2使用gii生成的搜索视图里的表单使用的是get方式,我改为post就提示无法验证,以为是控制器默认访问是get,实际默认是get和post都可以 public function behavio ...

  3. 解决windows系统80端口被占用问题(转)

    在windows下部署web应用(80端口),启动时提示bind 80端口失败 检查端口占用: netstat -ano | findstr 0.0.0.0:80 发现System进程 (pid=4) ...

  4. CXF bus interceptor配置

    作用:BUS是cxf的支架,它主要担当扩展及拦截器提供者的角色. 在这里主要讲讲 bus的interceptor的功能 目前配置cxf的interceptor主要有2中方法: 1.通过xml配置文件的 ...

  5. 【转】快速理解Kafka分布式消息队列框架

     from:http://blog.csdn.net/colorant/article/details/12081909 快速理解Kafka分布式消息队列框架 标签: kafkamessage que ...

  6. HttpUtility.UrlEncode 和 HttpUtility.Encode 一个大深坑

    进行了 Encode 之后 在解码(UrlEncode )之后 + 号莫名的变成了空格, 需要执行一个replace 操作

  7. 在linux命令行中直接执行php命令

    有时候用浏览器调试太麻烦,想在linux命令下直接执行php代码 php -r 'echo 0500;'

  8. 分词工具ICTCLAS5.0使用心得

    接触自然语言处理有一年多了,最基本的一些自然是分词,词性标注,命名实体识别之类的知识,有些应用知道原理是一回事,自己动手做起来又是另外一回事了.最近又开始重操旧业:分词.分词最著名的自然就是中科院的分 ...

  9. Genymotion启动时出现错误virtualization engine not found

    打开VirtualBox,管理-全局设定,网络,仅主机“Host-only”网络,需要的设置如下

  10. 2015年12月01日 GitHub入门学习(二)手把手教你Git安装

    序:Mac与Linux中,Mac都预装了Git,各版本的Linux也都提供了Git的软件包.下面手把手教你Windows下的安装. 一.Git Windows GUI 下载地址 msysgit htt ...