Description:

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.

Hint:

  1. Think of "looking ahead". You want to cache the next element.Show More Hint

Follow up: How would you extend your design to be generic and work with all types, not just integer?

Credits:
Special thanks to @porker2008 for adding this problem and creating all test cases.

就是利用Java中的Iterator接口来实现一个类,主要是peek()方法的实现与API中的不同,可以在peek()和next()中同时使用Iterator.next()来实现。

设置一个top来保存当前值。

 // Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
class PeekingIterator implements Iterator<Integer> { private Iterator<Integer> it; private Integer top = null; public PeekingIterator(Iterator<Integer> iterator) {
// initialize any member here.
it = iterator;
} // Returns the next element in the iteration without advancing the iterator.
public Integer peek() {
Integer peek;
if(top != null) {
peek = top;
}
else {
peek = top = next();
}
return peek;
} // hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
@Override
public Integer next() {
Integer next = 0;
if(top != null) {
next = top;
top = null;
}
else {
next = it.next();
}
return next;
} @Override
public boolean hasNext() {
if(top != null) {
return true;
}
return 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

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

  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. 多线程中的synchronized小结

    1.synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种: 1. 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调用这个 ...

  2. 系统优化 /etc/sysctl.conf

    # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl ...

  3. DataGridView使用技巧九:DataGridView的右键菜单(ContextMenuStrip)

    DataGridView,DataGridViewColumn,DataGridViewRow,DataGridViewCell有ContextMenuStrip属性.可以通过设置ContextMen ...

  4. laravel 视图与传参

    1:先建立好一个控制器HgjController,其中index方法 return  view('hgj'); 2: 建立视图 在resources/views/hgj.blad.php <ht ...

  5. 如何对抗、预防 SQL注入 攻击

    一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...

  6. 查询相应id下的数据

    ---恢复内容开始--- u方法这样的:带不起模板引擎 <a href="{:U('Del/wzxg','','')}/{$vo.id}">修改</a> 这 ...

  7. Java logger组件:slf4j, jcl, jul, log4j, logback, log4j2

    先说结论 建议优先使用logback 或 log4j2.log4j2 不建议和 slf4j 配合使用,因为格式转换会浪费性能. 名词:jcl 和 jul 标题中的 jcl 是 apache Jakar ...

  8. Supervision 行为模式

    官方链接:http://erlang.org/doc/man/supervisor.html http://erlang.org/doc/design_principles/sup_princ.htm ...

  9. 今天搞log4net插入错误日志去mysql数据库的时候出现了点问题,已解决。记录下解决方案

    先上图 配置log4net的时候要填这项,可是这个value我不知道啊.....上图里的value是我用下面的方法获取的 MySqlConnection con = new MySqlConnecti ...

  10. Servlet+Ajax实现搜索智能提示

    一般在百度搜索框输入关键词时,会弹出一些相关信息提示,方便点选: 页面(search.jsp): <input type="text" name="keyWords ...