• 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();
     */
  • 相关阅读:
    关于调试 SharePoint 2010 中的爬网问题的疑难解答提示
    JS 获取 本周、本月、本季度、本年、上月、上周、上季度、去年
    jqGrid动态列
    【原创】博客“缘”
    讀取內部網站8/22
    開啟VS2005速度慢8/25
    仲秋節快樂
    外部表不是預期的格式8/28
    要考試10/17
    DataTable導入Excel 8/27
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/9137889.html
Copyright © 2020-2023  润新知