leetcode [#341] | GCidea's blog
目录

题目
Given a nested list of integers, implement an iterator to flatten it.
Each element is either an integer, or a list – whose elements may also be integers or other lists.
Example:
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].
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].
解决方案
1 | /** |
注意事项
- 接口NestedInteger无需实现,可以直接调用相应方法。
- 私有方法getFlatternInteger用于对输入的List nestedList做展开并存入一个堆栈。对于是数字的项,直接存入堆栈,对于是List类型的元素,递归调用该方法。
- next()和hasNext()方法对栈做弹出和判空即可。