341. Flatten Nested List Iterator
341. Flatten Nested List Iterator
You are given a nested list of integers nestedList
. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.
Implement the NestedIterator
class:
NestedIterator(List<NestedInteger> nestedList)
Initializes the iterator with the nested listnestedList
.int next()
Returns the next integer in the nested list.boolean hasNext()
Returnstrue
if there are still some integers in the nested list andfalse
otherwise.
Example 1:
Input: nestedList = [[1,1],2,[1,1]]
Output: [1,1,2,1,1]
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
Example 2:
Input: nestedList = [1,[4,[6]]]
Output: [1,4,6]
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
Constraints:
1 <= nestedList.length <= 500
The values of the integers in the nested list is in the range
[-106, 106]
.
My Solutions:
方法1:因为要保持element的order,在constructor里从后往前遍历元素,加到stack里面,这样保证排在后面的element最后出。在hasNext里处理元素,如果最上面的元素不是integer,把他们从后往前加入到stack里。
/**
* // 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 empty list if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
public class NestedIterator implements Iterator<Integer> {
Stack<NestedInteger> stack = new Stack<>();
public NestedIterator(List<NestedInteger> nestedList) {
if (nestedList == null) return;
for (int i = nestedList.size() - 1; i >= 0; i--)
// 把NestedInteger push到stack里
stack.push(nestedList.get(i));
}
@Override
public Integer next() {
// 注意hasNext一定先被call,hasNext已经处理过了NestedInteger
return stack.pop().getInteger();
}
@Override
public boolean hasNext() {
while (!stack.isEmpty()) {
NestedInteger top = stack.peek();
if (top.isInteger()) return true;
else {
stack.pop();
for (int i = top.getList().size() - 1; i >= 0; i--) {
stack.push(top.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();
*/
方法2:写一个function先把所有元素都先加进一个list,用来存贮integer。用一个int指针代表已经next出到第几个元素。
public class NestedIterator implements Iterator<Integer> {
List<Integer> list;
int index;
public NestedIterator(List<NestedInteger> nestedList) {
if (nestedList == null) return;
this.list = new ArrayList<>();
this.index = 0;
for (NestedInteger n : nestedList) addIntegerToList(n);
}
public void addIntegerToList(NestedInteger n) {
if (n.isInteger()) this.list.add(n.getInteger());
else {
List<NestedInteger> tmp = n.getList();
for (NestedInteger tmpN : tmp) addIntegerToList(tmpN);
}
}
@Override
public Integer next() {
return this.list.get(index++);
}
@Override
public boolean hasNext() {
if (index < this.list.size()) return true;
else return false;
}
}
Last updated