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();
    }
};
相关推荐
局部有解1 小时前
Leetcode Hot100 - 最长连续序列
算法·leetcode·职场和发展
坤坤子吖1 小时前
Linux 进程信号入门——从 Ctrl+C 到 kill
linux·开发语言·c++·笔记·学习
java_nnnn2 小时前
LeetCode刷题滑动窗口(最大连续1的个数三+将x减到0的最小操作数)Java
算法·leetcode·职场和发展
bnmoel2 小时前
C++ 基础入门篇(六)类和对象-第三篇:运算符重载
开发语言·c++·算法·语法·运算符重载
小七在进步2 小时前
初识C++中的模板
java·开发语言·c++
垆边人似月.2 小时前
华为题:合并区间
数据结构·c++·算法
低频电磁之道3 小时前
C++ 中有符号与无符号类型混用可能导致的问题
开发语言·c++
垆边人似月.3 小时前
华为题:滑动窗口最大值
数据结构·c++·算法
菩提树下的凡夫3 小时前
OPenCV4.8与OpenCV5.0的区别
c++·opencv
要练八块腹肌3 小时前
map和set介绍与使用
c++