• 341. Flatten Nested List Iterator


    问题描述:

    Given a nested list of integers, implement an iterator to flatten it.

    Each element is either an integer, or a list -- whose elements may also be integers or other lists.

    Example 1:
    Given the list [[1,1],2,[1,1]],

    By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].

    Example 2:
    Given the list [1,[4,[6]]],

    By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].

    解题思路:

    这道题我们可以用栈来辅助解答

    设置一个存储NestedInteger的栈并将list里的对象全部压入栈中

    在hasNext方法中,我们需要判断的是,当前是否存在有效的对象。

    当前栈顶有三种可能:

      1.栈为空,显然这时hasNext返回false

      2.栈顶为对象:此时可以返回true

      3.栈顶为list:此时我们要把list拆解并重新入栈,直至栈顶为一个对象

    需要注意的是!

    若栈顶为list嵌套着空的list如:[ [], [] ]

    看起来像是一个list里嵌套了两个list,我们要对其进行拆解。

    代码:

    /**
     * // 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:
        NestedIterator(vector<NestedInteger> &nestedList) {
            pushToStack(nestedList);
        }
    
        int next() {
            NestedInteger list = stk.top();
            int ret = list.getInteger();
            stk.pop();
            return ret;
        }
    
        bool hasNext() {
            while(!stk.empty()){
                NestedInteger temp = stk.top();
                if(!temp.isInteger()){
                    stk.pop();
                    vector<NestedInteger> list = temp.getList();
                    if(!list.empty()){
                        pushToStack(list);
                    }
                }else{
                    return true;
                }
            }
            
            return false;
        }
    private:
        stack<NestedInteger> stk;
        void pushToStack(vector<NestedInteger> &list){
            int n = list.size();
            for(int i = n-1; i > -1; i--){
                stk.push(list[i]);
            }
        }
    };
    
    /**
     * Your NestedIterator object will be instantiated and called as such:
     * NestedIterator i(nestedList);
     * while (i.hasNext()) cout << i.next();
     */
  • 相关阅读:
    图片优化工具
    Mac显示和隐藏文件的命令
    C中调用LUA回调(LUA注册表)
    电信猫拨号再加路由器
    VMware NAT模式网络配置
    spring mvc 数据绑定
    银行存款余额调节表实例
    加强银行余额调节表在财务管理中的作用
    c语言typedef的用法-解惑阿!很多天书般的东西解释的不错(转)
    c语言指针详解(转载)
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/9137889.html
Copyright © 2020-2023  润新知