【LeetCode】284. Peeking Iterator
题目:
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.
提示:
这题我一开始用了一个queue来做,后来看了讨论板块才发现,其实Iterator类里是有一个拷贝构造函数的:Iterator(const Iterator& iter);
这样的话利用拷贝构造函数所返回的实例,就不用担心会影响游标的位置了。
代码:
// Below is the interface for Iterator, which is already defined for you.
// **DO NOT** modify the interface for Iterator.
class Iterator {
struct Data;
Data* data;
public:
Iterator(const vector<int>& nums);
Iterator(const Iterator& iter);
virtual ~Iterator();
// Returns the next element in the iteration.
int next();
// Returns true if the iteration has more elements.
bool hasNext() const;
}; class PeekingIterator : public Iterator {
public:
PeekingIterator(const vector<int>& nums) : Iterator(nums) {
// Initialize any member here.
// **DO NOT** save a copy of nums and manipulate it directly.
// You should only use the Iterator interface methods.
} // Returns the next element in the iteration without advancing the iterator.
int peek() {
return Iterator(*this).next();
} // hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
int next() {
return Iterator::next();
} bool hasNext() const {
return Iterator::hasNext();
}
private:
};
【LeetCode】284. Peeking Iterator的更多相关文章
- 【LeetCode】284. Peeking Iterator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/peeking-i ...
- 【LeetCode】900. RLE Iterator 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rle-itera ...
- 【LeetCode】281. Zigzag Iterator 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 deque 日期 题目地址:https://leetc ...
- 【leetcode】900. RLE Iterator
题目如下: 解题思路:非常简单的题目,直接递归就行了. 代码如下: class RLEIterator(object): def __init__(self, A): ""&quo ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- 新手在WindowsServer2016上安装ExchangeServer2016时的几点注意要点。
这两天试着在WindowsServer2016上安装ExchangeServer2016,遇到了两个头疼的问题,还好几经搜索加摸索终于把问题解决了,现在把经验分享出来,给遇到同样的问题的人以参考.在W ...
- Nginx安装lua支持
Nginx安装lua支持 需要LuaJIT-2.0.4.tar.gz,ngx_devel_kit,lua-nginx-module 1.下载安装LuaJIT-2.0.4.tar.gz wget -c ...
- yaf学习之——生成yaf示例框架
windows 下面的生成yaf示例框架 1,下载 https://github.com/laruence/yaf 2,解压以后得到yaf-master文件夹 3,cmd命令窗口cd到上面解压的文件夹 ...
- Swift学习笔记(4):字符串
目录: 初始化 常用方法或属性 字符串索引 初始化 创建一个空字符串作为初始值: var emptyString = "" // 空字符串字面量 var anotherEmptyS ...
- Java 多线程详解(三)------线程的同步
Java 多线程详解(一)------概念的引入:http://www.cnblogs.com/ysocean/p/6882988.html Java 多线程详解(二)------如何创建进程和线程: ...
- DropDownList如何绑定DataTable,如何绑定DataSet
dpDnUpMenu是我定义的DropDownList控件 如果直接使用下面的方式,则会出现如下错误 dpDnUpMenu.DataSource = menu_tbBll.GetPID() dpDnU ...
- 3、J2EE学习推荐书籍
3.J2EE学习推荐书籍 J2EE的学习应该循序渐进,一本好书会很快上手和深入.在学习J2EE之前,应该学好SQL,基本上,程序设计都会跟数据库打交道.如果SQL没学好,就如同房子没有基脚 ...
- js中new一个对象的过程
使用new关键字调用函数(new ClassA(-))的具体步骤: 1. 创建空对象{}: 2. 设置新对象的constructor属性为构造函数的名称,设置新对象的__proto__属性指向构造函数 ...
- Mac下配置Nginx负载均衡
1.首先在Mac下安装Nginx(可参考我的另一篇随笔http://www.cnblogs.com/malcolmfeng/p/6896703.html). 2.安装Tomcat,下载后,解压,bin ...
- 发布支持多线程的PowerShell模块 —— MultiThreadTaskRunner
应用场景 多线程在需要批量处理一些任务的时候相当有用,也更加有利于充分利用现有计算机的能力.所有主流的开发语言都支持多线程. 默认情况下,PowerShell作为一个脚本语言,是不支持多线程操作的,虽 ...