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();
    }
};
相关推荐
风筝在晴天搁浅3 小时前
快手/字节 CodeTop LeetCode 415.字符串相加
算法·leetcode
小黄人软件3 小时前
C++读写编辑CSV文件示例源码 用于数据导入导出,比Excel好使
开发语言·c++·excel
郭涤生3 小时前
C++各个版本的性能和安全性总结
开发语言·c++
wljy15 小时前
二、静态库的制作和使用
linux·c语言·开发语言·c++
道剑剑非道5 小时前
FFmpeg 6.0 实战:用 C++ 封装摄像头采集与 RTSP 推流
开发语言·c++·ffmpeg
米粒15 小时前
力扣算法刷题 Day 64 Floyd算法 & A* 算法 & 总结篇
算法·leetcode·职场和发展
光电笑映5 小时前
从环境变量到进程虚拟地址空间——Linux 内存管理的底层脉络
linux·服务器·c++·c
sparEE6 小时前
c++字符串和自定义字面量
开发语言·c++
蜡笔小马7 小时前
03.C++设计模式-原型模式
c++·设计模式·原型模式
神仙别闹7 小时前
基于QT(C++)实现线性表的建立、插入、删除、查找等基本操作
java·c++·qt