zoukankan      html  css  js  c++  java
  • 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();
     */
  • 相关阅读:
    js node 节点 原生遍历 createNodeIterator
    nodejs fs copy本地文件src dst
    axios 请求常用组件,及其错误
    【IntelliJ IDEA学习之三】IntelliJ IDEA常用快捷键
    【IntelliJ IDEA学习之二】IntelliJ IDEA常用配置
    【IntelliJ IDEA学习之一】IntelliJ IDEA安装激活、VM参数
    【python学习案例】python判断自身是否正在运行
    【Linux脚本学习案例】shell脚本多通道并发执行存储过程
    【Activiti学习之四】Activiti API(三)
    【Activiti学习之三】Activiti API(二)
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/9137889.html
Copyright © 2011-2022 走看看