← 返回 tesla 的题目列表Flatten Nested List Iterator
类型:online_judge
Problem: 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 lists. The nesting depth is arbitrary.
Implement an iterator that returns all integers in left-to-right order.
You need to support two operations:
next(): return the next integer.
hasNext(): return true if there is a next integer; otherwise return false.
In the interview, you may be asked to:
Implement it recursively first;
Then implement it using an explicit stack, avoiding full recursive flattening upfront.
Input/Output for Testing
For convenient testing, the input is a JSON-style nested array, and the output is the flattened integer array.
Constraints
Total number of integers n: 0 <= n <= 10^5.
Maximum nesting depth: 10^4.
Integer range: [-10^9, 10^9].
Example
Input:
[[1,1],2,[1,1]]
Output:
[1, 1, 2, 1, 1]
Example
Input
[[1,1],2,[1,1]]
Output
[1, 1, 2, 1, 1]