给你一个嵌套的整型列表。请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数。

列表中的每一项或者为一个整数,或者是另一个列表。其中列表的元素也可能是整数或是其他列表。

示例 1:

  1. 输入: [[1,1],2,[1,1]]
  2. 输出: [1,1,2,1,1]
  3. 解释: 通过重复调用 next 直到 hasNext 返回 falsenext 返回的元素的顺序应该是: [1,1,2,1,1]。

示例 2:

  1. 输入: [1,[4,[6]]]
  2. 输出: [1,4,6]
  3. 解释: 通过重复调用 next 直到 hasNext 返回 falsenext 返回的元素的顺序应该是: [1,4,6]。

题解

没看懂什么意思啊!o(╥﹏╥)o,主要是没认真看

深度优先搜索

嵌套的整型结构是一个树形结构,树上叶子节点对应一个整数,非叶节点对应一个列表。在这棵树上深度优先搜索的顺序就是迭代器遍历的顺序。可以先遍历整个嵌套列表,将所有整数存入一个数组,然后遍历该数组从而实现nexthasNext方法

JavaScript

  1. /**
  2. * // This is the interface that allows for creating nested lists.
  3. * // You should not implement it, or speculate about its implementation
  4. * function NestedInteger() {
  5. *
  6. * Return true if this NestedInteger holds a single integer, rather than a nested list.
  7. * @return {boolean}
  8. * this.isInteger = function() {
  9. * ...
  10. * };
  11. *
  12. * Return the single integer that this NestedInteger holds, if it holds a single integer
  13. * Return null if this NestedInteger holds a nested list
  14. * @return {integer}
  15. * this.getInteger = function() {
  16. * ...
  17. * };
  18. *
  19. * Return the nested list that this NestedInteger holds, if it holds a nested list
  20. * Return null if this NestedInteger holds a single integer
  21. * @return {NestedInteger[]}
  22. * this.getList = function() {
  23. * ...
  24. * };
  25. * };
  26. */
  27. /**
  28. * @constructor
  29. * @param {NestedInteger[]} nestedList
  30. */
  31. var NestedIterator = function(nestedList) {
  32. vals = [];
  33. const dfs = (nestedList) => {
  34. for (const nest of nestedList) {
  35. if (nest.isInteger()) {
  36. vals.push(nest.getInteger());
  37. } else {
  38. dfs(nest.getList());
  39. }
  40. }
  41. }
  42. dfs(nestedList);
  43. };
  44. /**
  45. * @this NestedIterator
  46. * @returns {boolean}
  47. */
  48. NestedIterator.prototype.hasNext = function() {
  49. return vals.length > 0;
  50. };
  51. /**
  52. * @this NestedIterator
  53. * @returns {integer}
  54. */
  55. NestedIterator.prototype.next = function() {
  56. const val = vals[0];
  57. vals = vals.slice(1);
  58. return val;
  59. };
  60. /**
  61. * Your NestedIterator will be called like this:
  62. * var i = new NestedIterator(nestedList), a = [];
  63. * while (i.hasNext()) a.push(i.next());
  64. */

Python

  1. # """
  2. # This is the interface that allows for creating nested lists.
  3. # You should not implement it, or speculate about its implementation
  4. # """
  5. #class NestedInteger:
  6. # def isInteger(self) -> bool:
  7. # """
  8. # @return True if this NestedInteger holds a single integer, rather than a nested list.
  9. # """
  10. #
  11. # def getInteger(self) -> int:
  12. # """
  13. # @return the single integer that this NestedInteger holds, if it holds a single integer
  14. # Return None if this NestedInteger holds a nested list
  15. # """
  16. #
  17. # def getList(self) -> [NestedInteger]:
  18. # """
  19. # @return the nested list that this NestedInteger holds, if it holds a nested list
  20. # Return None if this NestedInteger holds a single integer
  21. # """
  22. class NestedIterator:
  23. def __init__(self, nestedList: [NestedInteger]):
  24. self.values = []
  25. def dfs(list):
  26. for i in list:
  27. if i.isInteger():
  28. self.values.append(i.getInteger())
  29. else:
  30. dfs(i.getList())
  31. dfs(nestedList)
  32. def next(self) -> int:
  33. val = self.values[0]
  34. self.values = self.values[1:]
  35. return val
  36. def hasNext(self) -> bool:
  37. return len(self.values) > 0
  38. # Your NestedIterator object will be instantiated and called as such:
  39. # i, v = NestedIterator(nestedList), []
  40. # while i.hasNext(): v.append(i.next())

复杂度分析

  • 时间复杂度:初始化为 🥈 341. 扁平化嵌套列表迭代器 - 图1,next 和 hasNext 为 🥈 341. 扁平化嵌套列表迭代器 - 图2。其中 n 是嵌套的整型列表中的元素个数。
  • 空间复杂度:🥈 341. 扁平化嵌套列表迭代器 - 图3。需要一个数组存储嵌套的整型列表中的所有元素。