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();
    }
};
相关推荐
漫随流水5 分钟前
leetcode算法(145.二叉树的后序遍历)
数据结构·算法·leetcode·二叉树
2501_9418752813 分钟前
在东京复杂分布式系统中构建统一可观测性平台的工程设计实践与演进经验总结
c++·算法·github
漫随流水17 分钟前
leetcode算法(94.二叉树的中序遍历)
数据结构·算法·leetcode·二叉树
Jacen.L24 分钟前
SIGABRT (6) 中止信号详解
c++
王老师青少年编程44 分钟前
信奥赛C++提高组csp-s之并查集(案例实践)2
数据结构·c++·并查集·csp·信奥赛·csp-s·提高组
满天星83035771 小时前
【C++】特殊类设计
c++·windows
Ljubim.te2 小时前
inline介绍,宏定义的注意事项以及nullptr
c语言·开发语言·c++
苦藤新鸡2 小时前
6.三数之和
c语言·c++·算法·力扣
Frank_refuel2 小时前
C++之内存管理
java·数据结构·c++
leiming62 小时前
c++ qt开发第一天 hello world
开发语言·c++·qt