341. Flatten Nested List Iterator

仅供自己学习

 

思路:

这种列表嵌套列表的可以视作一个多叉树型结构,如果是单个整数就视为叶子节点,如果非叶子节点是就是非叶子节点。

扁平化嵌套列表迭代器意思是,需要设计一个迭代器,这个迭代器能把嵌套列表铺平成一个个的int,每次通过 hasNext函数来判断是否存在下一个整数,通过next返回下一个整数。

 

我们采用DFS递归+vector的形式,因为列表中嵌套列表,所以我们要通过DFS进入嵌套的列表里获取里面的每一个整型数字存到vector中,如果是整型数字直接加入vector后面即可。

定义一个迭代器,由于我们的列表里的元素都已经以int形式保存在vector中,我们只需要用迭代器遍历即可。

对于 isInteger,getInteger 都在类里面已经定义并说明

代码:

 1 /**
 2  * // This is the interface that allows for creating nested lists.
 3  * // You should not implement it, or speculate about its implementation
 4  * class NestedInteger {
 5  *   public:
 6  *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 7  *     bool isInteger() const;
 8  *
 9  *     // Return the single integer that this NestedInteger holds, if it holds a single integer
10  *     // The result is undefined if this NestedInteger holds a nested list
11  *     int getInteger() const;
12  *
13  *     // Return the nested list that this NestedInteger holds, if it holds a nested list
14  *     // The result is undefined if this NestedInteger holds a single integer
15  *     const vector<NestedInteger> &getList() const;
16  * };
17  */
18 
19 class NestedIterator {
20 private:
21     vector<int> val;
22     vector<int>::iterator cur;
23     void DFS(const vector<NestedInteger>& nestedList){
24         for(auto& next:nestedList){
25             if(next.isInteger()){
26                 val.push_back(next.getInteger());
27             }
28             else DFS(next.getList());
29         }
30     }
31 public:
32     NestedIterator(vector<NestedInteger> &nestedList) {
33         DFS(nestedList);
34         cur=val.begin();
35     }
36     
37     int next() {
38         return *cur++;
39     }
40     
41     bool hasNext() {
42         return cur != val.end();
43     }
44 };
45 
46 /**
47  * Your NestedIterator object will be instantiated and called as such:
48  * NestedIterator i(nestedList);
49  * while (i.hasNext()) cout << i.next();
50  */

 

有递归那就有迭代。我们用一个栈,栈的元素是一对迭代器,第一个迭代器用来指向当前遍历到的元素,第二个用来指向list的最后一个元素的+1位置。

对于next函数,因为我们每次调用之前都会调用hasnext函数,所以确保如果调用next函数就一定能返回元素,所以我们直接返回栈顶元素并向后移动一位。

对于hasnext元素,我们会进入循环,如果栈存的迭代器pair对中第一个迭代器移动与第二个迭代器重合,说明我们这个list遍历完成,这时候就pop掉这个list并且跳过剩下的循环。当我们第一个迭代器指向的是整型数字,那么就返回true,否则这个第一个迭代器指的只会是list了,那么就把通过 getlist 成员函数返回这个list并且将第一个迭代器移动到像一个元素,然后把返回的list加入进栈。

 

这里的emplace (vector.begin,vector.end)就是会形成pair再加入进栈。

这里迭代器移动只是针对于该list来移动,所以最大的list的第一个迭代器移动,[[1,1],2]他从第一个元素移动到第二个元素是从[1,1]移动到2,而不是从1,移动到1.

代码:

 1 /**
 2  * // This is the interface that allows for creating nested lists.
 3  * // You should not implement it, or speculate about its implementation
 4  * class NestedInteger {
 5  *   public:
 6  *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 7  *     bool isInteger() const;
 8  *
 9  *     // Return the single integer that this NestedInteger holds, if it holds a single integer
10  *     // The result is undefined if this NestedInteger holds a nested list
11  *     int getInteger() const;
12  *
13  *     // Return the nested list that this NestedInteger holds, if it holds a nested list
14  *     // The result is undefined if this NestedInteger holds a single integer
15  *     const vector<NestedInteger> &getList() const;
16  * };
17  */
18 
19 class NestedIterator {
20 private:
21     stack<pair<vector<NestedInteger>:: iterator,vector<NestedInteger>:: iterator>> st;
22     
23 public:
24     NestedIterator(vector<NestedInteger> &nestedList) {
25         st.emplace(nestedList.begin(),nestedList.end());
26     }
27     
28     int next() {
29         return st.top().first++->getInteger();
30     }
31     
32     bool hasNext() {
33         while(!st.empty()){
34             auto& a=st.top();
35             if(a.first==a.second){
36                 st.pop();
37                 continue;
38             }
39             if(a.first->isInteger()){
40                 return true;
41             }
42             auto& lst=a.first++->getList();
43             st.emplace(lst.begin(),lst.end());
44         }
45         return false;
46     }
47 };
48 
49 /**
50  * Your NestedIterator object will be instantiated and called as such:
51  * NestedIterator i(nestedList);
52  * while (i.hasNext()) cout << i.next();
53  */

 

还可通过一个栈或队列来实现迭代。我们这里用栈,将nestedList所有元素放进栈里,从后往前放,因为是栈。

对于next函数,因为hasnext已经判断是整数才会调用,所以next直接获取栈顶的元素,弹出并返回即可。

对于hasnext函数,同样判断如果栈非空就持续循环,如果栈顶元素是整数就返回true,否则就是嵌套的list,就弹出取出的的元素并将这个list的每个元素加入进栈里。while如果结束没有返回true那么就返回false

代码:

 1 /**
 2  * // This is the interface that allows for creating nested lists.
 3  * // You should not implement it, or speculate about its implementation
 4  * class NestedInteger {
 5  *   public:
 6  *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 7  *     bool isInteger() const;
 8  *
 9  *     // Return the single integer that this NestedInteger holds, if it holds a single integer
10  *     // The result is undefined if this NestedInteger holds a nested list
11  *     int getInteger() const;
12  *
13  *     // Return the nested list that this NestedInteger holds, if it holds a nested list
14  *     // The result is undefined if this NestedInteger holds a single integer
15  *     const vector<NestedInteger> &getList() const;
16  * };
17  */
18 
19 class NestedIterator {
20 private:
21     stack<NestedInteger> st;
22 public:
23     NestedIterator(vector<NestedInteger> &nestedList) {
24         for(int i=nestedList.size()-1;i>=0;--i){
25             st.push(nestedList[i]);
26         }
27     }
28     
29     int next() {
30         NestedInteger cur = st.top(); st.pop();
31         return cur.getInteger();
32     }
33     
34     bool hasNext() {
35         while(!st.empty()){
36             auto a=st.top();
37             if(a.isInteger()) return true;
38             st.pop();
39             for(int i=a.getList().size()-1;i>=0;--i){
40                 st.push(a.getList()[i]);
41             }
42         }
43         return false;
44     }
45 };
46 
47 /**
48  * Your NestedIterator object will be instantiated and called as such:
49  * NestedIterator i(nestedList);
50  * while (i.hasNext()) cout << i.next();
51  */

 

posted @ 2021-03-23 14:26  Mrsdwang  阅读(33)  评论(0)    收藏  举报