LeetCode(282) 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.
分析
实现迭代器。
AC代码
// 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())
{
isNext = true;
nextVal = Iterator::next();
}
else{
isNext = false;
}
return ret;
}
bool hasNext() const {
return isNext;
}
private:
bool isNext;
int nextVal;
};
LeetCode(282) Peeking Iterator的更多相关文章
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- mybatis 日期比较
entity @JSONField(name="prj_date",ordinal=13,format="yyyy-MM-dd") private Date p ...
- 关于FutureTask的探索
之前关于Java线程的时候,都是通过实现Runnable接口或者是实现Callable接口,前者交给Thread去run,后者submit到一个ExecutorService去执行. 然后知道了还有个 ...
- 关于float和double类型能表示的数据范围和精度分析
来自教材<计算机组成原理>p16 float:6--7位 double:15--16位 意思就是double类型的数据,你确实能表达出很大的数字,但是其只有15位是精确的. 1.计算机中, ...
- ruby 字符串常用方法学习
引用链接:http://www.blogjava.net/nkjava/archive/2010/01/03/308088.html 1,切片:silce, [ ]-----------------[ ...
- P3930 SAC E#1 - 一道大水题 Knight
TLE,额 ,有空再写吧. #include<queue> #include<cstdio> #include<vector> #include<algori ...
- SpringMVC02 AbstractController And MultiActionController
1.AbstractController 若处理器继承自AbstractController类,那么该控制器就具有了一些新功能.因为AbstractControll类还继承自一个父类WebConten ...
- pageContext.setAttribute的使用场合
由于页面间跳转以后,pageScope域失效,所以,关于pageContext.setAttribute和pageContext.getAttribute的使用(pagecontext作用域是page ...
- bjut193E 吃饭时的怪癖
题目: http://bjutacm.openjudge.cn/lianxi/193E/ 思路: n的所有质因数之和等于phi(n) * n / 2, phi(n)为欧拉函数. 实现: #includ ...
- 常用的图片相关方法,读取,保存,压缩,缩放,旋转,drawable转化
import android.content.Context; import android.content.res.AssetManager; import android.content.res. ...
- Android Framework中的Application Framework层介绍
Android的四层架构相比大家都很清楚,老生常谈的说一下分别为:Linux2.6内核层,核心库层,应用框架层,应用层.我今天重点介绍一下应用框架层Framework,其实也是我自己的学习心得. Fr ...