284. 顶端迭代器

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

示例:

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

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

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

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

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

// 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 cache = null; // 第一次peek时, 缓存迭代的元素 public PeekingIterator(Iterator<Integer> iter) {
iterator = iter;
} public Integer peek() {
if (cache == null)
cache = iterator.next();
return cache;
} @Override
public Integer next() {
if (cache == null)
return iterator.next();
Integer temp = cache;
cache = null;
return temp;
} @Override
public boolean hasNext() {
return cache != null || iterator.hasNext();
}
}

Java实现 LeetCode 284 顶端迭代器的更多相关文章

  1. Leetcode 284.顶端迭代器

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

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

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

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

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

  4. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  8. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. HMM-前向后向算法与实现

    目录 基本要素 HMM三大问题 概率计算问题 前向算法 后向算法 前向-后向算法 基本要素 状态 \(N\)个 状态序列 \(S = s_1,s_2,...\) 观测序列 \(O=O_1,O_2,.. ...

  2. 关于proteus仿真的串口问题

    以下四幅图都是关于串口中断的问题,串口中断需要一个接收或者发送数据的触发. 图一:因为由串口小助手发送的数据达到了单片机串口,所以引起了串口的中断. 图二:图一的大图. 图三:因为由串口小助手发送的数 ...

  3. [hdu4911]逆序对相关

    思路:由于只能交换相邻的数,所以每次最多减小1个逆序对(且如果存在逆序对那么肯定可以减小1个)!于是乎..就是统计逆序对的裸题了.树状数组或归并都行. #pragma comment(linker, ...

  4. [带符号大整数模板]vector版

    #include <iostream> #include <cstdio> #include <vector> #include <cstring> u ...

  5. 1008 Elevator (20分)

    1008 Elevator (20分) 题目: The highest building in our city has only one elevator. A request list is ma ...

  6. urldecode二次编码

    0x01 <?php if(eregi("hackerDJ",$_GET[id])) { echo("not allowed!"); exit(); } ...

  7. JSP+Servlet+JDBC+mysql实现的个人日记本系统

    项目简介 项目来源于:https://gitee.com/wishwzp/Diary 本系统基于JSP+Servlet+Mysql 一个基于JSP+Servlet+Jdbc的个人日记本系统.涉及技术少 ...

  8. java -> StringBuffer与StringBuilder类

    字符串缓冲区 StringBuffer类 在学习String类时,API中说字符串缓冲区支持可变的字符串,什么是字符串缓冲区呢?接下来我们来研究下字符串缓冲区. 查阅StringBuffer的API, ...

  9. zsy后台管理系统-架构设计

    Zsy框架总体架构设计 1.Mysql数据库,存储所有表的数据. 2.Zsy-基础项目(Zsy-Model,Zsy-Dao,Zsy-Service,Zsy-Web),基于SSM框架.项目功能包含基本的 ...

  10. Linux下几个与磁盘空间和文件尺寸相关的命令

    大家好,我是良许. 硬盘是计算机非常重要的一个部件,不管是代码,还是 UI .声音.文档,抑或是没人时偷偷看的小视频,都需要保存在硬盘里. 对于很多 Linux 服务器,会进行很多的编译操作.而编译操 ...