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();
    }
};
相关推荐
梵尔纳多11 分钟前
OpenGL 坐标映射
c++·图形渲染
大头流矢1 小时前
C++的类与对象·三部曲:初阶
开发语言·c++
AAA.建材批发刘哥2 小时前
03--C++ 类和对象中篇
linux·c语言·开发语言·c++·经验分享
峥无3 小时前
《二叉搜索树:动态数据管理的利器,平衡树的基石》
开发语言·c++·二叉搜索树
CoderCodingNo3 小时前
【GESP】C++五级真题(数论, 贪心思想考点) luogu-B4070 [GESP202412 五级] 奇妙数字
开发语言·c++·算法
AAA.建材批发刘哥3 小时前
04--C++ 类和对象下篇
linux·c++·经验分享·青少年编程
stolentime3 小时前
洛谷P4417 [COCI 2006/2007 #2] STOL 题解
c++·coci
CoderCodingNo4 小时前
【GESP】C++五级真题(数论考点) luogu-P11961 [GESP202503 五级] 原根判断
开发语言·c++
-西门吹雪4 小时前
c++线程之标准库的并行算法研究
c++
KiefaC4 小时前
【C++11】包装器及其应用
开发语言·c++