C++ | Leetcode C++题解之第341题扁平化嵌套列表迭代器

题目:

题解:

cpp 复制代码
class NestedIterator {
private:
    vector<int> vals;
    vector<int>::iterator cur;

    void dfs(const vector<NestedInteger> &nestedList) {
        for (auto &nest : nestedList) {
            if (nest.isInteger()) {
                vals.push_back(nest.getInteger());
            } else {
                dfs(nest.getList());
            }
        }
    }

public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        dfs(nestedList);
        cur = vals.begin();
    }

    int next() {
        return *cur++;
    }

    bool hasNext() {
        return cur != vals.end();
    }
};
相关推荐
kyle~14 分钟前
ROS2 --- WaitSet(等待集) 等待实体就绪,管理执行回调函数
大数据·c++·机器人·ros2
量子炒饭大师31 分钟前
【C++进阶】Cyber骇客的赛博血统上传——【面向对象之 继承 】一文带你搞懂面向对象编程的三要素之————继承
c++·dubbo·继承·面向对象编程
Tanecious.42 分钟前
蓝桥杯备赛:Day2-B3612 求区间和
c++·蓝桥杯
C+++Python43 分钟前
Linux/C++多进程
linux·运维·c++
stolentime1 小时前
通信题:洛谷P15942 [JOI Final 2026] 赌场 / Casino题解
c++·算法·洛谷·joi·通信题
XZHOUMIN1 小时前
【生成pdf格式的报告】
c++·pdf·mfc
elseif1231 小时前
浅谈 C++ 学习
开发语言·c++·学习
沛沛rh451 小时前
深入并发编程:从 C++ 到 Rust 的学习笔记
c++·笔记·学习·算法·rust
小CC吃豆子2 小时前
C/C++中 int 的最大最小值
c语言·开发语言·c++
欧米欧2 小时前
C++模板初阶
开发语言·c++