给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext()。设计并实现一个支持 peek() 操作的顶端迭代器 – 其本质就是把原本应由 next() 方法返回的元素 peek() 出来。
示例:
假设迭代器被初始化为列表 [1,2,3]。
调用 next() 返回 1,得到列表中的第一个元素。
现在调用 peek() 返回 2,下一个元素。在此之后调用 next() 仍然返回 2。
最后一次调用 next() 返回 3,末尾元素。在此之后调用 hasNext() 应该返回 false。
进阶:你将如何拓展你的设计?使之变得通用化,从而适应所有的类型,而不只是整数型?
思路:
定义一个值nextVal用来保存next的值,每一步判断当前节点的next是否存在,如果存在把next节点的值保存下来。
在调用next的时候返回这个nextVal,并更新next节点
在调用peek的时候直接返回nextVal。
AC代码:(C++)
class PeekingIterator : public Iterator {
private:
int nextVal;
bool isNext;
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.
isNext = false;
if (Iterator::hasNext()) {
isNext = true;
nextVal = Iterator::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 res = nextVal;
if (Iterator::hasNext()) {
isNext = true;
nextVal = Iterator::next();
} else {
isNext = false;
}
return res;
}
bool hasNext() const {
return isNext;
}
};