[LeetCode] 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.
Your code will be tested with the following pseudocode:
initialize iterator with nestedList res = [] while iterator.hasNext() append iterator.next() to the end of res return res
If res
matches the expected flattened list, then your code will be judged as correct.
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]
.
扁平化嵌套列表迭代器。
给你一个嵌套的整数列表 nestedList 。每个元素要么是一个整数,要么是一个列表;该列表的元素也可能是整数或者是其他列表。请你实现一个迭代器将其扁平化,使之能够遍历这个列表中的所有整数。
实现扁平迭代器类 NestedIterator :
NestedIterator(List<NestedInteger> nestedList) 用嵌套列表 nestedList 初始化迭代器。
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/problems/flatten-nested-list-iterator
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题意是给一个带有嵌套的列表,里面存的是整数。请你把它扁平化,满足当调用hasNext()函数的时候,能返回相应的数字。我这里提供两种做法,一种是递归,另一种用到stack。
首先是递归的做法。首先创建一个空的列表res和一个index变量,记录list遍历到的位置。因为是嵌套列表,所以当遍历input列表的时候,有可能是数字,也有可能不是。如果不是数字,则继续递归调用helper函数,去处理sub list。如果是数字,则把数字加入res列表。其他的部分就比较直观了。
时间O(n)
空间O(n)
Java实现
1 public class NestedIterator implements Iterator<Integer> { 2 List<Integer> list = new ArrayList<>(); // 普通List 3 int index = 0; 4 5 public NestedIterator(List<NestedInteger> nestedList) { 6 // 预处理,扁平化嵌套list 7 helper(nestedList); 8 } 9 10 public void helper(List<NestedInteger> nestedList) { 11 // 循环List中每一个对象 12 for (NestedInteger i : nestedList) { 13 // 如果是数字 14 if (i.isInteger()) { 15 // 将该数字加入普通List 16 list.add(i.getInteger()); 17 } else { 18 // 如果不是数字,递归子问题解决 19 helper(i.getList()); 20 } 21 } 22 } 23 24 @Override 25 public Integer next() { 26 if (!hasNext()) { 27 return null; 28 } 29 return list.get(index++); 30 } 31 32 @Override 33 public boolean hasNext() { 34 return index < list.size(); 35 } 36 } 37 38 /** 39 * Your NestedIterator object will be instantiated and called as such: 40 * NestedIterator i = new NestedIterator(nestedList); 41 * while (i.hasNext()) v[f()] = i.next(); 42 */
另一种做法是用到栈stack。既然用到stack了,而且stack是先进后出,所以试着从input list的尾部往头部遍历。如果遍历到的是数字integer,则把这个数字加入stack;如果遍历到的不是integer,那么在把这个list从stack里弹出的同时,也从其尾部往头部遍历,将遍历到的数字加入stack。
时间O(n)
空间O(n)
Java实现
1 public class NestedIterator implements Iterator<Integer> { 2 Stack<NestedInteger> stack; 3 4 public NestedIterator(List<NestedInteger> nestedList) { 5 stack = new Stack<>(); 6 for (int i = nestedList.size() - 1; i >= 0; i--) { 7 stack.push(nestedList.get(i)); 8 } 9 } 10 11 @Override 12 public Integer next() { 13 return stack.pop().getInteger(); 14 } 15 16 @Override 17 public boolean hasNext() { 18 while (!stack.isEmpty()) { 19 NestedInteger cur = stack.peek(); 20 if (cur.isInteger()) { 21 return true; 22 } 23 stack.pop(); 24 for (int i = cur.getList().size() - 1; i >= 0; i--) { 25 stack.push(cur.getList().get(i)); 26 } 27 } 28 return false; 29 } 30 } 31 32 /** 33 * Your NestedIterator object will be instantiated and called as such: 34 * NestedIterator i = new NestedIterator(nestedList); 35 * while (i.hasNext()) v[f()] = i.next(); 36 */