Leetcode 341. Flatten Nested List Iterator

来源:互联网 发布:手机网络诊断工具 编辑:程序博客网 时间:2024/05/17 05:52

Description:
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].

本题我使用了栈和递归的方法来解决。首先观察本题输入的数据结构的形式,可以发现对于每一个Nested List,其元素要么是一个“真”的元素,即一个数字;要么是一个子Nested List,那么对于每一个子Nested List,我们可以用处理父List相同的方式去处理,这符合递归的定义,故想到用递归来处理该问题。
不过在递归之外要考虑的一个问题就是在处理完一个子List后,怎么回到父List上继续处理?这就想到了使用栈来存储List的结构,每当要进入一个子List时,将其父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 {private:    vector<NestedInteger>* cur_list;    stack<vector<NestedInteger>*> list_stack;    stack<int> index_stack;    int cur_index;public:    NestedIterator(vector<NestedInteger> &nestedList) {        cur_list = &nestedList;        cur_index = 0;    }    int next() {        return (*cur_list)[cur_index++].getInteger();    }    bool hasNext() {        //当前List处理完毕        if(cur_index >= cur_list->size()) {            if(list_stack.empty()) {                return false;            }            while(!list_stack.empty()) {                vector<NestedInteger> * temp_list = list_stack.top();                list_stack.pop();                int temp_index = index_stack.top();                index_stack.pop();                if(temp_index < (int)temp_list->size() - 1) {                    cur_list = temp_list;                    cur_index = temp_index + 1;                    return hasNext();                }            }            return false;        }        //当前List未处理完,但遇到了当前List的子List        else if(!(*cur_list)[cur_index].isInteger()) {                list_stack.push(cur_list);                index_stack.push(cur_index);                cur_list = &(*cur_list)[cur_index].getList();                cur_index = 0;                return hasNext();        }        return true;    }};/** * Your NestedIterator object will be instantiated and called as such: * NestedIterator i(nestedList); * while (i.hasNext()) cout << i.next(); */
原创粉丝点击