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.

Peeking迭代器,题目不再赘述,代码如下:

 // 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.
this->next();
} // Returns the next element in the iteration without advancing the iterator.
int peek() {
return nextVal;
} // hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
int next() {
int ret = nextVal;
if(Iterator::hasNext()){
bNext = true;
nextVal = Iterator::next();
}else{
bNext = false;
}
return ret;
} bool hasNext() const {
return bNext;
}
private:
bool bNext;
int nextVal;
};

LeetCode OJ:Peeking Iterator(peeking 迭代器)的更多相关文章

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

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

  2. [LeetCode] 900. RLE Iterator RLE迭代器

    Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...

  3. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  4. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  5. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  6. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  7. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  8. Java之集合初探(二)Iterator(迭代器),collections,打包/解包(装箱拆箱),泛型(Generic),comparable接口

    Iterator(迭代器) 所有实现了Collection接口的容器都有一个iterator方法, 用来返回一个实现了Iterator接口的对象 Iterator对象称作迭代器, 用来方便的实现对容器 ...

  9. Iterator接口(迭代器)

    目录 前言 原理 方法 异常 Iterator接口(迭代器) 前言 一般遍历数组都是采用for循环或者增强for,这两个方法也可以用在集合框架,但是还有一种方法是采用迭代器遍历集合框架,它是一个对象, ...

  10. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

随机推荐

  1. 使用IDEA 创建Servlet 的时候,找不到javax.servlet

    使用IDEA 开发工具,创建Servlet 文件的时候,出现了下面的这种错误, 解决步骤如下: 第一步:点击 File 第二步:找到Project Structure,点击,然后按照下图顺序操作,添加 ...

  2. linux配置Nginx启动,停止

    Nginx 启动.重启.停止脚本   第一步 先运行命令关闭nginx sudo kill `cat /usr/local/nginx/logs/nginx.pid`   第二步 vi /etc/in ...

  3. Django学习笔记之django-debug-toolbar使用指南

    介绍 django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试信息,并在单击时显示有关面板内容的更多详细信息. github地址 文档地址 安装 pip3 in ...

  4. CSS Border(边框)

    CSS Border(边框) 一.CSS 边框属性 CSS边框属性允许你指定一个元素边框的样式和颜色. 示例效果: 二.边框样式 边框样式属性指定要显示什么样的边界. border-style属性用来 ...

  5. Python3.x:Selenium+PhantomJS爬取带Ajax、Js的网页

    Python3.x:Selenium+PhantomJS爬取带Ajax.Js的网页 前言 现在很多网站的都大量使用JavaScript,或者使用了Ajax技术.这样在网页加载完成后,url虽然不改变但 ...

  6. Spring提前加载与懒加载

    首先,Spring默认是提前加载,这意味着当项目启动,spring初始化,spring会把所有的扫描包下的 ,所有带spring 注解(@Component.@Repository.@Service. ...

  7. 20145327 《Java程序设计》第六周学习总结

    20145327 <Java程序设计>第六周学习总结 教材学习内容总结 父类中的方法: 流(Stream)是对「输入输出」的抽象,而「输入输出」是相对程序而言的. 标准输入输出: Syst ...

  8. Linux下安装SVN服务端

    安装 使用yum安装非常简单: yum install subversion 配置 2.1. 创建仓库 我们这里在/home下建立一个名为svn的仓库(repository),以后所有代码都放在这个下 ...

  9. 关于office word 应用程序下载配置

    Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} fai ...

  10. 动态 K th

    每一棵线段树是维护每一个序列前缀的值在任意区间的个数,如果还是按照静态的来做的话,那么每一次修改都要遍历O(n)棵树,时间就是O(2*M*nlogn)->TLE考虑到前缀和,我们通过树状数组来优 ...