LeetCode——Peeking Iterator
Description:
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:
- Think of "looking ahead". You want to cache the next element.Show More Hint
Follow up: How would you extend your design to be generic and work with all types, not just integer?
Credits:
Special thanks to @porker2008 for adding this problem and creating all test cases.
就是利用Java中的Iterator接口来实现一个类,主要是peek()方法的实现与API中的不同,可以在peek()和next()中同时使用Iterator.next()来实现。
设置一个top来保存当前值。
// Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
class PeekingIterator implements Iterator<Integer> { private Iterator<Integer> it; private Integer top = null; public PeekingIterator(Iterator<Integer> iterator) {
// initialize any member here.
it = iterator;
} // Returns the next element in the iteration without advancing the iterator.
public Integer peek() {
Integer peek;
if(top != null) {
peek = top;
}
else {
peek = top = next();
}
return peek;
} // hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
@Override
public Integer next() {
Integer next = 0;
if(top != null) {
next = top;
top = null;
}
else {
next = it.next();
}
return next;
} @Override
public boolean hasNext() {
if(top != null) {
return true;
}
return it.hasNext();
}
}
LeetCode——Peeking Iterator的更多相关文章
- [LeetCode] Peeking Iterator 顶端迭代器
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- LeetCode Peeking Iterator
原题链接在这里:https://leetcode.com/problems/peeking-iterator/ 题目: Given an Iterator class interface with m ...
- LeetCode OJ:Peeking Iterator(peeking 迭代器)
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- [LeetCode] 284. Peeking Iterator 瞥一眼迭代器
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- 【LeetCode】284. Peeking Iterator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/peeking-i ...
- 【LeetCode】284. Peeking Iterator
题目: Given an Iterator class interface with methods: next() and hasNext(), design and implement a Pee ...
- LeetCode(282) Peeking Iterator
题目 Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peek ...
- [LeetCode] Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- LeetCode Zigzag Iterator
原题链接在这里:https://leetcode.com/problems/zigzag-iterator/ 题目: Given two 1d vectors, implement an iterat ...
随机推荐
- Eclipse 中 Debug 模式跳转到 exitCurrentThread 的问题解决
问题描述: Debug 模式启动项目,断点跳转到exitCurrentThread 解决方法: 修改Eclipse 配置 [window]->[Preferences]->[Java]-& ...
- Extjs Ext.ux.IFrame的用法 以及父子窗口间函数相互调用
Extjs Ext.ux.IFrame的用法 以及父子窗口间函数相互调用 Ext.ux.IFrame Extjs官方提供的一个组件,可以很方便的使用. 这样就完成了一个简单的IFrame的使用,通过E ...
- 【安装Python环境】之安装Selenium2时报UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 12: invalid continuation byte问题
问题描述: windows8.1系统,Python3环境安装Selenium2时报错,错误如下: ..... ..... File "F:\软件\python3.6.1\lib\site-p ...
- Linux mysql5.5
1.假设已经有mysql-5.5.10.tar.gz以及cmake-2.8.4.tar.gz两个源文件 (1)先安装cmake(mysql5.5以后是通过cmake来编译的) [root@ rhel5 ...
- .net 高级写法总结
1.处理HTTP非正常的请求参数: [1] 获取相应的流转为string [2] request 的只读属性设置为可编辑,类似form [3] 转换为json对象 [4] 重设只读属性 //判断请求类 ...
- SecureCRT工具
技巧收集: 文本文件内容 复制该行内容yy,p粘贴 2+yy复制两行 dd 删除该行 文件内容搜索 非编辑状态/+查找内容 查找指定行 :+行号
- get提交
<?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends Action { //显示用户 public function index(){ $Use ...
- 关于C语言底层
1.C语言和java C语言难在需要清楚底层的实现原理一错可能就内存泄露之类语法不难,原理很难 C语言和java数组是指一段连续的内存空间一个int 4字节一旦声明必须确定长度无法变长数组在数据结构里 ...
- C# VS本地Sqlserver 操作笔记
1.如何连接本地数据库 -- string Info = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirecto ...
- mysql没有my.ini文件
解决方法: 上面的任意一个文件拷贝一份,重命名my.ini.