Code & Func
2018-02-14

第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
class NestedIterator {
2
public:
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 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;
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的指针,利用这个指针来对下一级列表的元素进行迭代:

1
class NestedIterator {
2
public:
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
}
24
private:
25
vector<NestedInteger>::iterator _it, _end_it;
26
NestedIterator *_tmp_it;
27
};

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

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

1
class NestedIterator {
2
public:
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
30
private:
31
stack<vector<NestedInteger>::iterator> begins, ends;
32
};

update at 2020-04-03

和第二个解法有点像,但是只需要一个栈即可:

1
class NestedIterator {
2
public:
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
};
上一条动态