Leetcode算法学习日志-341 Flatten Nested List Iterator

Leetcode 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].

一些要用到的借口函数

/**
 * // 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;
 * };
 */

输出函数调用方法如下:

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

题意分析

定义一个迭代类型,将一个NestedInteger类数据中的所有数展开来输出,如果一个NestedInteger类数据只是一个int,则函数isInteger()返回1,且可以用 int getInteger() 来的到这个数,如果一个NestedInteger类数据是一个nested list,则可以用vector<NestedInteger> &getList()返回这个list。

解法分析

本题和二叉搜索树输出递增序列一样,都采用了DFS的思想, 将list中每个元素看成一个节点,如果节点时list,则它有多个子节点,因为本题是一个迭代器,意思就是代替递归的调用过程,用一个stack来保存深度优先搜索中经过的每个节点。本题将所有list中元素按从右向左的顺序放入stack,pop栈顶,如果是int,则hasnext为1,输出相应值,不然将pop出的list中每个元素按从右向左的顺序放入栈中,继续上述pop过程,next()函数就用来输出stack中pop出的第一个integer。本题的关键就是stack的构造,这种深度优先搜索的题目常常需要建立stack。C++代码如下:

class NestedIterator {
private:
    stack<NestedInteger> myStack;
public:
    NestedIterator(vector<NestedInteger> &nestedList) {//构造函数进行最初的stack设计。
        int i;
        for(i=nestedList.size()-1;i>=0;i--){
            myStack.push(nestedList[i]);
        }
        
    }
    int next() {//it shoul return one int at a time
        int res=myStack.top().getInteger();
        myStack.pop();
        return res;
    }

    bool hasNext() {
        while(!myStack.empty()){
              if(myStack.top().isInteger())
                 return true;
              else{
                 vector<NestedInteger> temp;
                 temp=myStack.top().getList();
                 myStack.pop();
                 int i;
                 for(i=temp.size()-1;i>=0;i--)
                    myStack.push(temp[i]);
                  }
        }
        return false; 
    }
};




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值