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();
    }
};
相关推荐
清酒难咽14 分钟前
算法案例之分治法
c++·经验分享·算法
小屁猪qAq14 分钟前
强符号和弱符号及应用场景
c++·弱符号·链接·编译
wen__xvn16 分钟前
代码随想录算法训练营DAY25第七章 回溯算法 part04
算法·leetcode·深度优先
头发还没掉光光20 分钟前
HTTP协议从基础到实战全解析
linux·服务器·网络·c++·网络协议·http
June bug1 小时前
(#字符串处理)字符串中第一个不重复的字母
python·leetcode·面试·职场和发展·跳槽
jojo_zjx1 小时前
GESP 24年12月2级 数位和
c++
自由的好好干活1 小时前
PCI9x5x驱动移植支持PCI9054在win7下使用3
c++·驱动开发
AlenTech2 小时前
197. 上升的温度 - 力扣(LeetCode)
算法·leetcode·职场和发展
源代码•宸3 小时前
Leetcode—404. 左叶子之和【简单】
经验分享·后端·算法·leetcode·职场和发展·golang·dfs
WBluuue3 小时前
数据结构与算法:dp优化——优化尝试和状态设计
c++·算法·leetcode·动态规划