Question

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.

Solution

Key to the solution is to cache status and value ahead.

 // Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
class PeekingIterator implements Iterator<Integer> {
private Iterator<Integer> iterator;
private int current;
private int peekValue;
private boolean hasNext; public PeekingIterator(Iterator<Integer> iterator) {
// initialize any member here.
this.iterator = iterator;
hasNext = this.iterator.hasNext();
if (hasNext)
peekValue = this.iterator.next();
} // Returns the next element in the iteration without advancing the iterator.
public Integer peek() {
return peekValue;
} // hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
@Override
public Integer next() {
current = peekValue;
hasNext = iterator.hasNext();
// Note here we need check hasNext, otherwise it will report runtime error
if (hasNext)
peekValue = iterator.next();
return current;
} @Override
public boolean hasNext() {
return hasNext;
}
}

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. Peeking Iterator

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

  4. LeetCode Peeking Iterator

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

  5. 284. Peeking Iterator

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

  6. Zigzag Iterator 解答

    Question Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...

  7. Binary Search Tree Iterator 解答

    Question Implement an iterator over a binary search tree (BST). Your iterator will be initialized wi ...

  8. 【LeetCode】284. Peeking Iterator

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

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

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

随机推荐

  1. UESTC_Dividing Numbers CDOJ 1156

    Dividing Numbers Time Limit: 9000/3000MS (Java/Others)     Memory Limit: 262144/262144KB (Java/Other ...

  2. IOS开发错误提示原因集合-----长期更新

    "[__NSCFConstantString size]: unrecognized selector sent to instance." =>将NSString类型的参数 ...

  3. ./scripts/feeds update -a OpenWrt大招系列

    ./scripts/feeds update -a Updating feed 'packages' from 'https://github.com/openwrt/packages.git' .. ...

  4. C++中单链表的建立和操作

    准备数据 准备在链表操作中需要用到的变量及数据结构 示例代码如下: struct Data //数据结点类型 { string key; //关键字 string name; int age; }; ...

  5. 《Java程序员面试笔试宝典》之Java程序初始化的顺序是怎样的

    在Java语言中,当实例化对象时,对象所在类的所有成员变量首先要进行初始化,只有当所有类成员完成初始化后,才会调用对象所在类的构造函数创建对象. Java程序的初始化一般遵循以下三个原则(以下三原则优 ...

  6. iOS-网络编程(一)HTTP协议

    一. 网络编程基础 在移动互联网时代,几乎所有应用都需要用到网络,只有通过网络跟外界进行数据交互.数据更新,应用才能保持新鲜.活力.一个好的移动网络应用不仅要有良好的UI和良好的用户体验也要具备实时更 ...

  7. (3)选择元素——(6)属性选择器(Attribute selectors)

    Attribute selectors are a particularly helpful subset of CSS selectors. They allow us to specify an ...

  8. 移动支付之智能IC卡与Android手机进行NFC通信

    本文来自http://blog.csdn.net/hellogv/ .引用必须注明出处.        眼下常见的智能IC卡执行着JavaCard虚拟机.智能IC卡上能够执行由精简后的Java语言编写 ...

  9. IOS中的各种Picker

    简述 在应用的一些设置中经常要用到一些Picker来快速帮助用户选定取值,一般会用到的有UIDatePicker,UIPickerView以及UIImagePickerController. 初始界面 ...

  10. Android 打造自己的个性化应用(一):应用程序换肤主流方式的分析与概述

    Android平台api没有特意为换肤提供一套简便的机制,这可能是外国的软件更注重功能和易用,不流行换肤.系统不提供直接支持,只能自行研究. 换肤,可以认为是动态替换资源(文字.颜色.字体大小.图片. ...