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();
    }
};
相关推荐
试剂小课堂 Pro9 小时前
mPEG-Silane:mPEG链单端接三乙氧基硅的亲水性硅烷偶联剂
java·c语言·网络·c++·python·tomcat
mjhcsp9 小时前
P14992 取模(题解)
c++
sin_hielo9 小时前
leetcode 3510
数据结构·算法·leetcode
老歌老听老掉牙10 小时前
16宫格属性分析系统:打造专业级科学数据可视化工具
c++·qt·可视化
橘子师兄10 小时前
C++AI大模型接入SDK—API接入大模型思路
开发语言·数据结构·c++·人工智能
CSDN_RTKLIB10 小时前
【字符编码】拷贝的是字符还是字节序列
c++
历程里程碑10 小时前
哈希3 : 最长连续序列
java·数据结构·c++·python·算法·leetcode·tornado
闻缺陷则喜何志丹10 小时前
【图论】P9661 [ICPC 2021 Macao R] Sandpile on Clique|普及+
c++·算法·图论·洛谷
2401_8414956410 小时前
【LeetCode刷题】两两交换链表中的节点
数据结构·python·算法·leetcode·链表·指针·迭代法
无尽的罚坐人生10 小时前
hot 100 560.和为 K 的子数组
数据结构·算法·leetcode