Code & Fun

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

挺有趣的题目,主要是要实现一个嵌套列表的迭代器,大概是三个函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class NestedIterator {
public:
NestedIterator(vector<NestedInteger> &nestedList) {

}

int next() {

}

bool hasNext() {

}
};

然后他也提供了NestedInteger的接口和一些说明,算是对题目的补充:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* // 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();
*/

从补充中我们可以看到,在调用next前一定会先调用hasNext,有了这个前提我们写起来会方便一点。

我的解法是,NestedIterator只保存构造函数中传入的nestedList的两个迭代器,之所以是两个,是因为要保存end迭代器,然后要实现嵌套,我们还要一个NestedIterator的指针,利用这个指针来对下一级列表的元素进行迭代:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class NestedIterator {
public:
NestedIterator(vector<NestedInteger> &nestedList) {
_it = nestedList.begin(); _end_it = nestedList.end();
_tmp_it = nullptr;
}

int next() {
if (_it->isInteger()) { int ret = _it->getInteger(); ++_it; return ret; }
return _tmp_it->next();
}

bool hasNext() {
if (_it == _end_it) return false;
if (_it->isInteger()) return true;

if (_tmp_it == nullptr) {
_tmp_it = new NestedIterator(_it->getList());
}
if (_tmp_it->hasNext()) return true;
delete _tmp_it; _tmp_it = nullptr; ++_it;
return hasNext();
}
private:
vector<NestedInteger>::iterator _it, _end_it;
NestedIterator *_tmp_it;
};

然后这里的实现虽然比较简单,简洁,但是在遇到一些特殊情况的时候会对性能造成极大的影响,比如说[[[[[1,2,3]]]]],虽然只有三个元素,但是因为有5层的嵌套,我们要有5个迭代器,每次调用nexthasNext都需要递归调用5次才能返回,这样效率就有点低了.

dicuss中的解法会比较好一点,类别DFS来做,先用stack保存所有的元素,在调用hasNext的时候,如果栈顶是列表就将其展开并压栈(倒序),然后在递归调用hasNext,直到栈顶为数字时,然后调用next就直接返回栈顶即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class NestedIterator {
public:
NestedIterator(vector<NestedInteger> &nestedList) {
begins.push(nestedList.begin());
ends.push(nestedList.end());
}

int next() {
hasNext();
return (begins.top()++)->getInteger();
}

bool hasNext() {
while (begins.size()) {
if (begins.top() == ends.top()) {
begins.pop();
ends.pop();
} else {
auto x = begins.top();
if (x->isInteger())
return true;
begins.top()++;
begins.push(x->getList().begin());
ends.push(x->getList().end());
}
}
return false;
}

private:
stack<vector<NestedInteger>::iterator> begins, ends;
};

本文首发于Code & Fun