284. 窥探迭代器
作者:互联网
284. 窥探迭代器
题目描述
思路分析
设计题
给类增加一个缓存位置即可。
代码实现
/*
* 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);
*
* // 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:
int cur;
bool has_next;
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.
has_next=Iterator::hasNext();
if(has_next) cur=Iterator::next();
}
// Returns the next element in the iteration without advancing the iterator.
int peek() {
return cur;
}
// hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
int next() {
int t=cur;
has_next=Iterator::hasNext();
if(has_next) cur=Iterator::next();
return t;
}
bool hasNext() const {
return has_next;
}
};
标签:hasNext,迭代,Iterator,int,next,const,窥探,284,cur 来源: https://blog.csdn.net/qq_50757994/article/details/121434987