C++ | Leetcode C++题解之第284题窥视迭代器

题目:

题解:

cpp 复制代码
template <class T>
class PeekingIterator : public Iterator<T> {
public:
    PeekingIterator(const vector<T>& nums) : Iterator<T>(nums) {
        flag = Iterator<T>::hasNext();
        if (flag) {
            nextElement = Iterator<T>::next();
        }
    }
    
    T peek() {
        return nextElement;
    }

    T next() {
        T ret = nextElement;
        flag = Iterator<T>::hasNext();
        if (flag) {
            nextElement = Iterator<T>::next();
        }
        return ret;
    }
    
    bool hasNext() const {
        return flag;
    }
private:
    T nextElement;
    bool flag;
};
相关推荐
Sw1zzle3 小时前
算法入门(四):二叉树 - 递归遍历三件套
算法·leetcode
海石4 小时前
子树怎么找?树的3种遍历方式来帮忙!
算法·leetcode
海石4 小时前
难度分 1588:思路 + 技巧 = AC
算法·leetcode
殳翰4 小时前
下服务器端开发流程及相关工具介绍(C++)
开发语言·c++
青瓦梦滋5 小时前
协议定制/序列化-反序列化(Linux视角)
linux·服务器·网络·c++
山登绝顶我为峰 3(^v^)38 小时前
C/C++ 交叉编译方法
java·c语言·c++
郝学胜-神的一滴12 小时前
中级OpenGL教程 013:渲染器类架构设计与逐帧渲染流程详解
开发语言·c++·unity·游戏引擎·图形渲染·opengl·unreal
Frostnova丶12 小时前
(12)LeetCode 76. 最小覆盖子串
算法·leetcode·职场和发展
wabs66613 小时前
关于动态规划【力扣583.两个字符串的删除操作的思考】
算法·leetcode·动态规划