题目:

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.
  2. Is one variable sufficient? Why or why not?
  3. Test your design with call order of peek() before next() vs next() before peek().
  4. For a clean implementation, check out Google's guava library source code.

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

链接: http://leetcode.com/problems/peeking-iterator/

题解:

设计一个带peek()的Iterator。这里我们根据题意和提示,提前cached下一个元素就可以了。这里我用了一个boolean变量 peekUsed来判断我们是否使用过peek(),其实也可以不用。在用过peek()之后,我们只返回cache过的element,而且hasNext() = true。否则我们使用正常的iterator的methods。

Time Complexity - O(1), Space Complexity - O(1)

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

二刷:

使用Integer来做global variable的话就可以省略使用boolean了

Java:

// Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
class PeekingIterator implements Iterator<Integer> {
private Iterator<Integer> iter;
private Integer next; 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 res = next;
next = iter.hasNext() ? iter.next() : null;
return res;
} @Override
public boolean hasNext() {
return next != null;
}
}

Reference:

https://leetcode.com/discuss/59368/concise-java-solution

https://leetcode.com/discuss/59327/my-java-solution

https://leetcode.com/discuss/62829/simple-java-solution-by-caching-next-element

284. Peeking Iterator的更多相关文章

  1. 【LeetCode】284. Peeking Iterator

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

  2. [LeetCode] 284. Peeking Iterator 瞥一眼迭代器

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

  3. 【LeetCode】284. Peeking Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/peeking-i ...

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

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

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

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

  6. Peeking Iterator

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

  7. LeetCode Peeking Iterator

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

  8. Peeking Iterator 解答

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

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

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

随机推荐

  1. eclipse格式化代码末班修改

    在窗口->首选项->输入format(格式)搜索,或者找Java->代码样式->格式化程序: 几个内置的不能调格式化代码风格,但是可以根据内置的新建一个,出来很多选项,开始调吧 ...

  2. UAT测试,PPT测试

    UAT:user acceptable testing 用户验收测试 PPT:product produce test  产品生产验证

  3. strlen和mb_strlen区别

    转自:http://blog.sina.com.cn/s/blog_5f0d5bd90100mzcl.html <?php//测试时文件的编码方式要是UTF8$str='中文a字1符';echo ...

  4. JS 学习笔记--4---运算符

    1.JS 中包含的运算符有:一元运算符.二元运算符.三元运算符.算术运算符.关系运算符.逻辑运算符.位运算符.赋值运算符.其他的运算符等. 2.表达式:简单来讲就是一句代码(分号隔开),解释器会把它翻 ...

  5. idea maven添加jar包

    在“项目结构“里设置 选择libaray,添加jar包

  6. 2012 Asia Chengdu Regional Contest

    Browsing History http://acm.hdu.edu.cn/showproblem.php?pid=4464 签到 #include<cstdio> #include&l ...

  7. xcodebuild和xcrun实现自动打包iOS应用程序

    随着苹果手持设备用户的不断增加,ios应用也增长迅速,同时随着iphone被越狱越来越多的app 的渠道也不断增多,为各个渠道打包成了一件费时费力的工作,本文提供一种比较智能的打包方式来减少其带来的各 ...

  8. HTML5 Cheat sheet PNG帮助手册(标签、事件、兼容)

    HTML5 Cheat sheet PNG帮助手册(标签.事件.兼容) 1.HTML5标签 2.HTML5事件 3.HTML5兼容 最新HTML5手册资料请参考:http://www.inmotion ...

  9. 索引服务混战ASP.NET――微软的又一个隔离墩

    话说月初笔者在华山之巅搞定了ASP.NET一起莫名奇妙的异常,自此之后和公主过着…嘟--,不好意思,书都看杂了,串了台了.好,咱们闲言少叙,书归正传. 自从上次解决了由调试文件库引起的ASP.NET执 ...

  10. 十个实用但IE不支持的CSS属性

    对IE浏览器尤其是IE6的抱怨基本已进入麻痹状态,偶尔甚至产生非常消极的想法:这个世界只有一个浏览器就好了,哪怕这唯一的浏览器就是IE6.当然,这样的想法是非常病态的,马上打消.本文里面,介绍了10个 ...