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();
    }
};
相关推荐
郝学胜-神的一滴2 小时前
Leetcode 969 煎饼排序✨:翻转间的数组排序艺术
数据结构·c++·算法·leetcode·面试
炸膛坦客9 小时前
单片机/C/C++八股:(二十)指针常量和常量指针
c语言·开发语言·c++
I_LPL9 小时前
hot100贪心专题
数据结构·算法·leetcode·贪心
炸膛坦客10 小时前
单片机/C/C++八股:(十九)栈和堆的区别?
c语言·开发语言·c++
2401_8318249611 小时前
代码性能剖析工具
开发语言·c++·算法
是wzoi的一名用户啊~11 小时前
【C++小游戏】2048
开发语言·c++
Sunshine for you12 小时前
C++中的职责链模式实战
开发语言·c++·算法
qq_4160187212 小时前
C++中的状态模式
开发语言·c++·算法
2401_8845632412 小时前
模板代码生成工具
开发语言·c++·算法
2401_8319207412 小时前
C++代码国际化支持
开发语言·c++·算法