文档章节

284. Peeking Iterator

52iSilence7
 52iSilence7
发布于 10/21 22:25
字数 281
阅读 6
收藏 0

Description

Tag: Design Difficulties: Medium

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().

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.
Follow up: How would you extend your design to be generic and work with all types, not just integer?

Solution

// 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 Integer next;
    private boolean cache = false;
	public PeekingIterator(Iterator<Integer> iterator) {
	    // initialize any member here.
	    this.iterator = iterator;
	}

    // Returns the next element in the iteration without advancing the iterator.
	public Integer peek() {
        if (!cache && hasNext()) {
            cache = true;
            next = iterator.next();
        }
        return next;
	}

	// hasNext() and next() should behave the same as in the Iterator interface.
	// Override them if needed.
	@Override
	public Integer next() {
	    if (cache) {
            cache = false;
            return next;
        }
        return iterator.next();
	}

	@Override
	public boolean hasNext() {
	    if (cache) {
            return true;
        }
        return iterator.hasNext();
	}
}

Note: Only check next is null or not cannot determine whether there is next or not. A list can contain null integers. So it is false design.

© 著作权归作者所有

共有 人打赏支持
52iSilence7
粉丝 4
博文 69
码字总数 50566
作品 0
海淀
高级程序员
私信 提问
实现迭代器 Peeking Iterator

问题: Given an Iterator class interface with methods: and , design and implement a PeekingIterator that support the operation -- it essentially peek() at the element that will ......

叶枫啦啦
2017/12/19
0
0
开发的app 在小米手机上不能运行

这是在下面手机上运行的报错日志: 10-11 11:37:49.524: E/art(31965): Throwing OutOfMemoryError "Failed to allocate a 36 byte allocation with 0 free bytes and 0B until OOM" 10-11 1......

晚秋枫叶
2016/10/11
816
2
ALGO-118 算法训练 友好数

  9的约数和有:1+3=4   4的约数和有:1+2=3   所以9和4不是友好的。   220的约数和有:1 2 4 5 10 11 20 22 44 55 110=284   284的约数和有:1 2 4 71 142=220   所以220和284...

xnh_565175944
04/26
0
0
一个播客(podcast)展示对用户程序的攻击

接上周博文中叙述:一个攻击者可以利用操作系统的安全弱点对用户程序的内存数据进行非授权式的访问. 以下是用一个视频播客来展示这样一个攻击. 在此攻击展示中,用户Alice正在用字符编辑程序v...

晨曦之光
2012/03/09
0
0
【转】Ceph对象存储(rgw)的IPv6环境配置

引言 在搭建成功Ceph集群后,对于如何使用,其实我还是一脸MB的,心想竟然提供三种存储接口(对象,文件,快),口气也未免太大。在结合项目需求之后,我选择了对象存储接口。那么问题又来了...

lemon2013
05/11
0
0

没有更多内容

加载失败,请刷新页面

加载更多

在Flutter中嵌入Native组件的解决方案

摘要: 引言 在漫长的从Native向Flutter过渡的混合工程时期,要想平滑地过渡,在Flutter中使用Native中较为完善的控件会是一个很好的选择。本文希望向大家介绍AndroidView的使用方式以及在此...

阿里云官方博客
37分钟前
1
0
aws S3 util demo

package com.example.demo;import com.amazonaws.AmazonClientException;import com.amazonaws.AmazonServiceException;import com.amazonaws.auth.BasicAWSCredentials;import co......

经常把天聊死的胖子
56分钟前
4
0
linux下查看cpu、memo、io、swap性能数据脚本

直接贴脚本: 1、cpu #!/bin/bashCurrentDate=`date -d today '+%Y%m%d'`CurrentTime=`date -d today '+%Y%m%d%H%M'`mytext="$CurrentTime\t`top -b -n 1 | grep Cpu\(s\......

郑加威
今天
5
0
MySQL之——查询重复记录、删除重复记录方法大全

MySQL之——查询重复记录、删除重复记录方法大全

安小乐
今天
2
0
spring容器启动,停止,关闭事件监听-ApplicationEvent

ApplicationEvent ApplicationEvent相当于一个事件,所有自定义事件都需要继承这个抽象类。在Eclipse中Ctrl+Shift+H调用类的层次结构列表,可以看到如下 Application下抽象子类ApplicationCo...

tantexian
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部