第100天。
今天的题目是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].
挺有趣的题目,主要是要实现一个嵌套列表的迭代器,大概是三个函数:
1class NestedIterator {2public:3 NestedIterator(vector<NestedInteger> &nestedList) {4
5 }6
7 int next() {8
9 }10
11 bool hasNext() {12
13 }14};
然后他也提供了NestedInteger
的接口和一些说明,算是对题目的补充:
1/**2 * // This is the interface that allows for creating nested lists.3 * // You should not implement it, or speculate about its implementation4 * 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 integer10 * // The result is undefined if this NestedInteger holds a nested list11 * int getInteger() const;12 *13 * // Return the nested list that this NestedInteger holds, if it holds a nested list14 * // The result is undefined if this NestedInteger holds a single integer15 * const vector<NestedInteger> &getList() const;9 collapsed lines
16 * };17 */18
19
20/**21 * Your NestedIterator object will be instantiated and called as such:22 * NestedIterator i(nestedList);23 * while (i.hasNext()) cout << i.next();24 */
从补充中我们可以看到,在调用next
前一定会先调用hasNext
,有了这个前提我们写起来会方便一点。
我的解法是,NestedIterator
只保存构造函数中传入的nestedList
的两个迭代器,之所以是两个,是因为要保存end
迭代器,然后要实现嵌套,我们还要一个NestedIterator
的指针,利用这个指针来对下一级列表的元素进行迭代:
1class NestedIterator {2public:3 NestedIterator(vector<NestedInteger> &nestedList) {4 _it = nestedList.begin(); _end_it = nestedList.end();5 _tmp_it = nullptr;6 }7
8 int next() {9 if (_it->isInteger()) { int ret = _it->getInteger(); ++_it; return ret; }10 return _tmp_it->next();11 }12
13 bool hasNext() {14 if (_it == _end_it) return false;15 if (_it->isInteger()) return true;12 collapsed lines
16
17 if (_tmp_it == nullptr) {18 _tmp_it = new NestedIterator(_it->getList());19 }20 if (_tmp_it->hasNext()) return true;21 delete _tmp_it; _tmp_it = nullptr; ++_it;22 return hasNext();23 }24private:25 vector<NestedInteger>::iterator _it, _end_it;26 NestedIterator *_tmp_it;27};
然后这里的实现虽然比较简单,简洁,但是在遇到一些特殊情况的时候会对性能造成极大的影响,比如说[[[[[1,2,3]]]]]
,虽然只有三个元素,但是因为有5层的嵌套,我们要有5个迭代器,每次调用next
和hasNext
都需要递归调用5次才能返回,这样效率就有点低了.
dicuss
中的解法会比较好一点,类别DFS
来做,先用stack
保存所有的元素,在调用hasNext
的时候,如果栈顶是列表就将其展开并压栈(倒序),然后在递归调用hasNext
,直到栈顶为数字时,然后调用next
就直接返回栈顶即可:
1class NestedIterator {2public:3 NestedIterator(vector<NestedInteger> &nestedList) {4 begins.push(nestedList.begin());5 ends.push(nestedList.end());6 }7
8 int next() {9 hasNext();10 return (begins.top()++)->getInteger();11 }12
13 bool hasNext() {14 while (begins.size()) {15 if (begins.top() == ends.top()) {17 collapsed lines
16 begins.pop();17 ends.pop();18 } else {19 auto x = begins.top();20 if (x->isInteger())21 return true;22 begins.top()++;23 begins.push(x->getList().begin());24 ends.push(x->getList().end());25 }26 }27 return false;28 }29
30private:31 stack<vector<NestedInteger>::iterator> begins, ends;32};
update at 2020-04-03
和第二个解法有点像,但是只需要一个栈即可:
1class NestedIterator {2public:3 stack<vector<NestedInteger>::iterator> st;4 NestedIterator(vector<NestedInteger> &nestedList) {5 reversePush(nestedList);6 }7
8 void reversePush(vector<NestedInteger> &nestedList) {9 auto beg = nestedList.begin();10 for(int i = nestedList.size() - 1; i >= 0; --i) {11 st.push(beg + i);12 }13 }14
15 int next() {12 collapsed lines
16 auto top = st.top(); st.pop();17 return top->getInteger();18 }19
20 bool hasNext() {21 while(!st.empty() && !st.top()->isInteger()) {22 auto top = st.top(); st.pop();23 reversePush(top->getList());24 }25 return !st.empty();26 }27};