[Copy question]:
Given a nested list of integers, implement an iterator to flatten it.
Each element was either an integer, or a list--whose elements may also be integers or other lists.
Example 1:
Given the list [[1,1],2,[1,1]]
,
By calling next repeatedly until hasnext returns FALSE, the order of elements returned by next should be: [1,1,2,1,1]
.
Example 2:
Given the list [1,[4,[6]]]
,
By calling next repeatedly until hasnext returns FALSE, the order of elements returned by next should be: [1,4,6]
.
[Brute force solution]:
Time Analysis:
Spatial Analysis:
[After optimization]:
Time Analysis:
Spatial Analysis:
[Wonderful output CONDITIONS]:
[Wonderful corner case]:
[Thinking questions]:
Don't know what the difference is between next and. Hasnext: Take it out and just see if there's
[a sentence of thought]:
[input]: null: Normal: Large: Extra Small: Special cases handled in the program: abnormal conditions (unreasonable input):
[Drawing]:
[One brush]:
[Two brushes]:
[Three brushes]:
[Four brushes]:
[Five brushes]:
[Results of five-minute visual debug]:
[Summary]:
[Complexity]:time Complexity:o () Space complexity:o ()
[English data structure or algorithm, why not other data structures or algorithms]:
Only the stack can be removed one layer at a time, and the array cannot be removed directly from one layer. So use stack.
Stack has
.getInteger()
.getList()
Method
[Algorithmic thinking: Recursion/division/greed]:
[Key templating code]:
[Other solutions]:
[Follow up]:
[The problem given by the LC becomes variable]:
[Code Style]:
341. Flatten Nested List iterator Expand multilayer arrays