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();
    }
};
相关推荐
And_Ii10 小时前
LCR 168. 丑数
c++
CoderMeijun10 小时前
C++ 时间处理与格式化输出:从 Linux 时间函数到 Timestamp 封装
c++·printf·stringstream·时间处理·clock_gettime
XiYang-DING14 小时前
【LeetCode】Hash | 136.只出现一次的数字
算法·leetcode·哈希算法
tankeven14 小时前
HJ176 【模板】滑动窗口
c++·算法
OxyTheCrack14 小时前
【C++】一文详解C++智能指针自定义删除器(以Redis连接池为例)
c++·redis
whitelbwwww14 小时前
C++基础--类型、函数、作用域、指针、引用、文件
开发语言·c++
leaves falling14 小时前
C/C++ const:修饰变量和指针的区别(和引用底层关系)
c语言·开发语言·c++
tod11315 小时前
深入解析ext2文件系统架构
linux·服务器·c++·文件系统·ext
不想写代码的星星15 小时前
C++ 类型萃取:重生之我在幼儿园修炼类型学
c++
比昨天多敲两行15 小时前
C++11新特性
开发语言·c++