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();
    }
};
相关推荐
syagain_zsx6 分钟前
jsoncpp 使用指南:编译链接、Json::Value 取值与高频坑
c++·json·库·http格式
Nil20820 分钟前
LeetCode 283移动零
数据结构·算法·leetcode
布莱克6051 小时前
理解 = delete 与 = default:两种赋值语义的差异
c++
文丑颜不良啊2 小时前
LeetCode226~250题目解析
java·leetcode
小小、码农2 小时前
[特殊字符]【C++11】类的新功能全面解析:移动构造、移动赋值、default、delete、final 与 override
linux·开发语言·网络·c++
库玛西2 小时前
外部排序全解析:败者树、置换-选择排序与最佳归并树
c语言·数据结构·c++·笔记·考研·算法
General_G4 小时前
创建型模式—原型(Prototype)模式
c++·设计模式·原型模式
syagain_zsx4 小时前
protobuf 实战:从 .proto 到 C++ 序列化的完整链路
c++·学习·程序人生·谷歌·protobuf
welfare9625 小时前
新号别搞:C++_list
开发语言·c++
xiaoye-duck5 小时前
《C++ 项目:手写大模型接入 SDK》云端 API 调用基础与 Apifox 调试实战
网络·c++·语言模型