AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

LeetCode 341. Flatten Nested List Iterator    原题链接    中等

作者: 作者的头像   Diamondz ,  2019-10-26 13:23:03 ,  所有人可见 ,  阅读 1289


12


1

题目描述

给定一个嵌套的整型列表。设计一个迭代器,使其能够遍历这个整型列表中的所有整数。

列表中的项或者为一个整数,或者是另一个列表。

样例

示例1

输入: [[1,1],2,[1,1]]
输出: [1,1,2,1,1]
解释: 通过重复调用next 直到hasNext 返回false,next返回的元素的顺序应该是: [1,1,2,1,1]。

示例2

输入: [1,[4,[6]]]
输出: [1,4,6]
解释: 通过重复调用next直到hasNext 返回false,next返回的元素的顺序应该是: [1,4,6]。

算法1

(DFS存储压平后的链表) $O(n)$

访问迭代器的过程可以用递归来描述,当我们访问一个NestedInteger迭代器时,如果它包含的不是一个整数,即isInteger是false,那么我们对于getList里的每一个NestedInteger迭代器再进行递归的访问,直到遇到一个整数为止,然后将它加入全局数组中。这样在构造函数初始化这个类的时候我们就已经将整个列表压平了。

时间复杂度

若整数的个数为n,那么压平这个列表的时间为$O(n)$,同时空间复杂度也为$O(n)$。

C++ 代码

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * class NestedInteger {
 *   public:
 *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     bool isInteger() const;
 *
 *     // Return the single integer that this NestedInteger holds, if it holds a single integer
 *     // The result is undefined if this NestedInteger holds a nested list
 *     int getInteger() const;
 *
 *     // Return the nested list that this NestedInteger holds, if it holds a nested list
 *     // The result is undefined if this NestedInteger holds a single integer
 *     const vector<NestedInteger> &getList() const;
 * };
 */
class NestedIterator {
public:
    vector<int> nums;
    int cnt = 0;
    NestedIterator(vector<NestedInteger> &nestedList) {
        dfs(nestedList);
    }

    void dfs(vector<NestedInteger> &nestedList) {
        for (auto list : nestedList) {
            if (list.isInteger()) nums.push_back(list.getInteger());
            else dfs(list.getList());
        }
    }

    int next() {
        return nums[cnt ++ ];
    }

    bool hasNext() {
        return cnt < nums.size();
    }
};

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i(nestedList);
 * while (i.hasNext()) cout << i.next();
 */

算法2

(用栈来模拟递归) $O(n)$

这种做法的思路与 LeetCode 173. Binary Search Tree Iterator 类似,当调用hasNext时我们将栈顶元素变为下一个要访问的整数,初始化时将迭代器倒序压入栈中,这样可以保证栈顶的迭代器为输入数组的第一个迭代器。

时间复杂度

时间复杂度与上面的算法相同,不过空间复杂度会降低。考虑[1,[1,[1,[1,...,]]]]这种情况,那么栈中的迭代器数量最大为2,降低了很多空间。

C++ 代码

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * class NestedInteger {
 *   public:
 *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     bool isInteger() const;
 *
 *     // Return the single integer that this NestedInteger holds, if it holds a single integer
 *     // The result is undefined if this NestedInteger holds a nested list
 *     int getInteger() const;
 *
 *     // Return the nested list that this NestedInteger holds, if it holds a nested list
 *     // The result is undefined if this NestedInteger holds a single integer
 *     const vector<NestedInteger> &getList() const;
 * };
 */
class NestedIterator {
public:
    stack<NestedInteger> stk;
    NestedIterator(vector<NestedInteger> &nestedList) {
        for (int i = nestedList.size() - 1; i >= 0; i -- ) {
            stk.push(nestedList[i]);
        }
    }

    int next() {
        int res = stk.top().getInteger();
        stk.pop();
        return res;
    }

    bool hasNext() {
        while(stk.size()) {
            auto t = stk.top();
            if (t.isInteger()) return true;
            stk.pop();

            for (int i = t.getList().size() - 1; i >= 0; i -- ) {
                stk.push(t.getList()[i]);
            } 
        }
        return false;
    }
};

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i(nestedList);
 * while (i.hasNext()) cout << i.next();
 */

0 评论

App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息