顶端迭代器

给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext()。设计并实现一个支持 peek() 操作的顶端迭代器 -- 其本质就是把原本应由 next() 方法返回的元素 peek() 出来。

示例:

假设迭代器被初始化为列表 [1,2,3]

调用 next() 返回 1,得到列表中的第一个元素。

现在调用 peek() 返回 2,下一个元素。在此之后调用 next() 仍然返回 2

最后一次调用 next() 返回 3,末尾元素。在此之后调用 hasNext() 应该返回 false

进阶:你将如何拓展你的设计?使之变得通用化,从而适应所有的类型,而不只是整数型?

[思路]

设一个 peeked 的flag 同时 保存 peeked 过得值.  如果已经peeked过了, next()直接返回保存的值即可.

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

Leetcode 284.顶端迭代器的更多相关文章

  1. Java实现 LeetCode 284 顶端迭代器

    284. 顶端迭代器 给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext().设计并实现一个支持 peek() 操作的顶端迭代器 – 其本质就是把原本应由 next() 方法 ...

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

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

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

    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 5123. 字母组合迭代器 Iterator for Combination

    地址 https://leetcode-cn.com/contest/biweekly-contest-15/problems/iterator-for-combination/ 题目描述请你设计一个 ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. C#LeetCode刷题-设计

    设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制   33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...

  8. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  9. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

随机推荐

  1. 水题 Codeforces Round #303 (Div. 2) A. Toy Cars

    题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...

  2. 题解报告:poj 1985 Cow Marathon(求树的直径)

    Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...

  3. 自己写的MD5加密原码

    package com.wh.md5; import java.security.MessageDigest; import java.util.Arrays; /** * @author 王恒 * ...

  4. Mysql数据类型简介(大概了解)

    知道有整型,浮点型,定点数类型( DECIMAL(M,D)M是数据总长度,是小数位 ),日期类,字符串类,二进制类型(存图片路径,视频路径一般用BLOG就行了喔)……不会再去查 讲一下几个专有名词: ...

  5. 151 Reverse Words in a String 翻转字符串里的单词

    给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...

  6. Myisamchk使用

    Myisam损坏的情况: . 服务器突然断电导致数据文件损坏;强制关机,没有先关闭mysql 服务;mysqld 进程在写表时被杀掉.因为此时mysql可能正在刷新索引. . 磁盘损坏. . 服务器死 ...

  7. shell随机数生成

    shell中的RANDOM变量: echo  $RANDOM 加上系统时间更加随机:echo `date +%N`$RANDOM | md5sum |cut -c1-8 通过/dev/urandom ...

  8. Spark网络通信分析

    之前分析过spark RPC的基本流程(spark RPC详解),其实无论是RPC还是Spark内部的数据(Block)传输,都依赖更底层的网络通信,本文将对spark的网络通信做一下剖析. 1,概要 ...

  9. Java基础50题test4—分解质因数

    [分解质因数] 题目:将一个正整数分解质因数.例如:输入 90,打印出 90=2*3*3*5. 程序分析:对 n 进行分解质因数,应先找到一个最小的质数 k,然后按下述步骤完成: (1)如果这个质数恰 ...

  10. phpstorm中快速添加函数注释

    Preferences 或 command+,快捷键 Live Templates - PHP 下方 - 新建模板 ,Abbreviation 命名随便写,点击Edit Variables配置变量信息 ...