给你一个嵌套的整数列表 nestedList 。每个元素要么是一个整数,要么是一个列表;该列表的元素也可能是整数或者是其他列表。请你实现一个迭代器将其扁平化,使之能够遍历这个列表中的所有整数。
实现扁平迭代器类 NestedIterator :
NestedIterator(List
int next() 返回嵌套列表的下一个整数。
boolean hasNext() 如果仍然存在待迭代的整数,返回 true ;否则,返回 false 。
你的代码将会用下述伪代码检测:
initialize iterator with nestedList
res = []
while iterator.hasNext()
append iterator.next() to the end of res
return res
如果 res 与预期的扁平化列表匹配,那么你的代码将会被判为正确。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/flatten-nested-list-iterator
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
递归
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class NestedIterator implements Iterator<Integer> {
private List<Integer> realList;
private Iterator<Integer> cur;
public NestedIterator(List<NestedInteger> nestedList) {
this.realList = new ArrayList<>();
dfs(nestedList);
cur = realList.iterator();
}
@Override
public Integer next() {
return cur.next();
}
@Override
public boolean hasNext() {
return cur.hasNext();
}
private void dfs(List<NestedInteger> nestedList) {
for (NestedInteger nestedInteger : nestedList) {
if (nestedInteger.isInteger()) {
realList.add(nestedInteger.getInteger());
} else {
dfs(nestedInteger.getList());
}
}
}
}
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/
// This is the interface that allows for creating nested lists.
// You should not implement it, or speculate about its implementation
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 empty list if this NestedInteger holds a single integer
public List<NestedInteger> getList();
}
迭代
import java.util.*;
public class NestedIterator implements Iterator<Integer> {
private Deque<Iterator<NestedInteger>> stack;
public NestedIterator(List<NestedInteger> nestedList) {
stack = new LinkedList<>();
stack.push(nestedList.iterator());
}
@Override
public Integer next() {
return stack.peek().next().getInteger();
}
@Override
public boolean hasNext() {
while (!stack.isEmpty()) {
Iterator<NestedInteger> integerIterator = stack.peek();
if (!integerIterator.hasNext()) {
stack.pop();
continue;
}
NestedInteger next = integerIterator.next();
if (next.isInteger()) {
stack.push(new ArrayList<NestedInteger>() {{
add(next);
}}.iterator());
return true;
}
stack.push(next.getList().iterator());
}
return false;
}
}
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/
// This is the interface that allows for creating nested lists.
// You should not implement it, or speculate about its implementation
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 empty list if this NestedInteger holds a single integer
public List<NestedInteger> getList();
}