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();
    }
};
相关推荐
y = xⁿ18 分钟前
20天速通LeetCode day07:前缀和
数据结构·算法·leetcode
小雅痞30 分钟前
[Java][Leetcode hard] 42. 接雨水
java·开发语言·leetcode
t***54435 分钟前
Dev-C++中哪些选项可以设置
开发语言·c++
2301_803554521 小时前
C++ 并发核心:std::promise、std::future、std::async 超详细全解
开发语言·c++
EverestVIP1 小时前
C++ 成员函数的指针
c++
俺不要写代码1 小时前
线程启动、结束,创建线程多法、join,detach,线程的移动语义
服务器·开发语言·网络·c++
思麟呀2 小时前
应用层协议HTTP
linux·服务器·网络·c++·网络协议·http
小徐不徐说2 小时前
面试C++易错点总结
开发语言·c++·面试·职场和发展·程序设计·工作
北顾笙9802 小时前
day26-数据结构力扣
数据结构·算法·leetcode
xier_ran3 小时前
【C++】“内部”、“外部”、“派生类”、“友元“类
java·开发语言·c++