题目:

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. Linux下各硬件装置的文件名

    Linux对计算机各组件/装置的分辨,是和Windows系统完全不一样的!因为各个组件/设备在Linux上都是一个文件!因此,我们在认识各项设备时一定要学习Linux的设备文件名. 众所周知,Linu ...

  2. Mysql高级之存储过程

    参考地址1:http://www.2cto.com/database/201411/350819.html 参考地址2:http://www.jb51.net/article/39471.htm my ...

  3. 【Count and Say】cpp

    题目: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111 ...

  4. OS X 使用技巧——在Finder窗口标题栏上显示路径

    Finder窗口默认显示当前文件夹的名称或当前所在的模式(例如AirDrop).如果想要显示路径(用User/[当前用户账号名称]/Documents 替代以前显示的Documents),打开终端并运 ...

  5. 数码管字符产生器GenSym 1.0发布

    本软件可以实现以下功能: 1.支持共阴极和共阳极数码管的字符代码的生成. 2.支持C语言和ASM语言方式产生字符串代码的序列. 3.可定制数码管的最高位和最低位的代码产生次序. 4.支持记忆功能,可以 ...

  6. IOS 打包后安装崩溃,debug正常运行

    今天遇到个奇葩问题,archive后的包安装后有一个crash,必崩的.但是调试跟踪时是好的. 为了方便调试,使用了release模式,这样不用每次都archive后安装进行测试.由于没法运行时deb ...

  7. 【转】Basic C# OOP Concept

    This Article will explain a very simple way to understand the basic C# OOP Concept Download ShanuBas ...

  8. JS的基础语法

    8.运算符号表达式 ①数学运算符 数学运算符有+.-.*./除().%(余数) var a = 10; var b = 5; alert(a+b); 预览以后在网页上弹出的对话框数值就是15. ②逻辑 ...

  9. Codeforces Round #204 (Div. 2)->C. Jeff and Rounding

    C. Jeff and Rounding time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. Nodejs Express 4.X 中文API 4--- Router篇

    相关阅读: Express 4.X API 翻译[一] --  Application篇 Express4.XApi 翻译[二] --  Request篇 Express4.XApi 翻译[三] -- ...