341. Flatten Nested List Iterator
https://leetcode.com/problems/flatten-nested-list-iterator/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 callingnextrepeatedly untilhasNextreturns false, the order of elements returned bynextshould be:
[1,1,2,1,1]
.Example 2: Given the list
[1,[4,[6]]]
,By callingnextrepeatedly untilhasNextreturns false, the order of elements returned bynextshould be:
[1,4,6]
.
Thoughts
实现针对nested的list的iterator,效果和遍历展开了的list一样。处理nested用stack。不过做这题时思路被另一道二维list iterator解法带偏了,总想着要用指针。实际并不需要,倒着把所有元素都压入栈,hasNext遍历栈顶时当遇到nested的元素则把它展开后重新入栈。
Code
/*
* @lc app=leetcode id=341 lang=cpp
*
* [341] Flatten Nested List Iterator
*/
// @lc code=start
/**
* // 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 {
public:
stack<NestedInteger> s;
NestedIterator(vector<NestedInteger> &nestedList) {
for (int i = nestedList.size() - 1; i >= 0; --i) {
s.push(nestedList[i]);
}
}
int next() {
int r = s.top().getInteger();
s.pop();
return r;
}
bool hasNext() {
while (!s.empty()) {
if (s.top().isInteger()) return true;
const auto t = s.top().getList(); s.pop();
for (int i = t.size() - 1; i >= 0; --i) {
s.push(t[i]);
}
}
return false;
}
};
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i(nestedList);
* while (i.hasNext()) cout << i.next();
*/
// @lc code=end
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
public class NestedIterator implements Iterator<Integer> {
Stack<NestedInteger> stack;
public NestedIterator(List<NestedInteger> nestedList) {
stack = new Stack();
for (int i = nestedList.size() - 1; i >= 0; i--) {
stack.push(nestedList.get(i));
}
}
@Override
public Integer next() {
return stack.pop().getInteger();
}
@Override
public boolean hasNext() {
while (!stack.isEmpty()) {
if (stack.peek().isInteger()) {
return true;
}
List<NestedInteger> nestedList = stack.pop().getList();
for (int i = nestedList.size() - 1; i >= 0; i--) {
stack.push(nestedList.get(i));
}
}
return false;
}
}
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/
Analysis
每个元素进出一次栈,时空复杂度O(N).
Last updated
Was this helpful?