Solution1
We could use a stack to perform the iteration.
In the constructor we push the elements of nestedListinto the stack from back to front. So when we call pop(), the very first element of nestedList is returned. Afterwards, in the hasNext function, we check the top element in the stack. If it is a Integer, return true. Otherwise, we take out every element of the list and push them to the stack from back to front (flatten again). Until we find an Integer. The reason to do so is that there may be a lot of nested empty lists. Thus until we actually find an integer, we are not sure if there is a next integer.
Suppose the number of element we have to check, in the nestedList, is n. Then the time complexity is O(n).
/**
* // 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> {
Deque<NestedInteger> stack;
public NestedIterator(List<NestedInteger> nestedList) {
this.stack = new ArrayDeque<>();
for (int i = nestedList.size() - 1; i >= 0; i--) {
stack.push(nestedList.get(i));
}
}
@Override
public Integer next() {
if (!this.hasNext()) {
return null;
}
return stack.pop().getInteger();
}
@Override
public boolean hasNext() {
while (!stack.isEmpty()) {
NestedInteger curr = stack.pop();
if (curr.isInteger()) {
stack.push(curr);
return true;
} else {
for (int i = curr.getList().size() - 1; i >= 0; i--) {
stack.push(curr.getList().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();
*/
Solution2
Rather than perform the flatten process on the fly, we could flatten the nextedList completely in the constructor.
Use a flattenHelper() function to flatten the nextedList recursively into a flattenedList. And thus we could construct a Iterator<Integer> based on the flattenedList we got.
This is actually quicker than the previous one although they share the asymptotic time and space complexity. This could due to that a real Iterator<Integer> is more efficient.
public class NestedIterator implements Iterator<Integer> {
private List<Integer> flattenedList;
private Iterator<Integer> it;
public NestedIterator(List<NestedInteger> nestedList) {
this.flattenedList = new ArrayList<Integer>();
flattenHelper(nestedList);
this.it = this.flattenedList.iterator();
}
private void flattenHelper(List<NestedInteger> nestedList) {
for (NestedInteger i : nestedList) {
if (i.isInteger()) {
this.flattenedList.add(i.getInteger());
} else {
flattenHelper(i.getList());
}
}
}
@Override
public Integer next() {
return this.it.next();
}
@Override
public boolean hasNext() {
return this.it.hasNext();
}
}

本文介绍两种实现嵌套列表迭代的方法:一是使用栈进行迭代,二是预先完全展平嵌套列表。第一种方法按需展平元素,适用于不确定嵌套深度的情况;第二种方法则在构造时完成展平工作,提高后续迭代效率。
215

被折叠的 条评论
为什么被折叠?



