341. Flatten Nested List Iterator

https://leetcode.com/problems/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 list nestedList.

  • int next() Returns the next integer in the nested list.

  • boolean hasNext() Returns true if there are still some integers in the nested list and false 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].

Solution:

https://mp.weixin.qq.com/s/uEmD5YVGG5LHQEmJQ2GSfw Version 1: DFS

首先,现在有一种数据结构NestedInteger这个结构中存的数据可能是一个Integer整数,也可能是一个NestedInteger列表。注意,这个列表里面装着的是NestedInteger,也就是说这个列表中的每一个元素可能是个整数,可能又是个列表,这样无限递归嵌套下去……

NestedInteger有如下 API:

我们的算法会被输入一个NestedInteger列表,我们需要做的就是写一个迭代器类,将这个带有嵌套结构NestedInteger的列表「拍平」:

我们写的这个类会被这样调用,先调用hasNext方法,后调用next方法

Image

比如示例 1,输入的列表里有三个NestedInteger,两个列表型的NestedInteger和一个整数型的NestedInteger

学过设计模式的朋友应该知道,迭代器也是设计模式的一种,目的就是为调用者屏蔽底层数据结构的细节,简单地通过hasNextnext方法有序地进行遍历。

为什么说这个题目很有启发性呢?因为我最近在用一款类似印象笔记的软件,叫做 Notion(挺有名的)。这个软件的一个亮点就是「万物皆 block」,block 其实就是一种数据结构,比如说标题、页面、表格都是 block。有的 block 甚至可以无限嵌套,这就打破了传统笔记本「文件夹」->「笔记本」->「笔记」的三层结构。

回想这个算法问题,NestedInteger结构实际上也是一种支持无限嵌套的结构,而且可以同时表示整数和列表两种不同类型,我想 Notion 的核心数据结构 block 估计也是这样的一种设计思路。

那么话说回来,对于这个算法问题,我们怎么解决呢?NestedInteger结构可以无限嵌套,怎么把这个结构「打平」,为迭代器的调用者屏蔽底层细节,扁平化地输出所有整数元素呢?

二、解题思路

显然,NestedInteger这个神奇的数据结构是问题的关键,不过题目专门提醒我们:

我不应该去尝试实现NestedInteger这个结构,也不应该去猜测它的实现?为什么?凭什么?是不是题目在误导我?是不是我进行推测之后,这道题就不攻自破了?

你看,labuladong 可不是什么好孩子,你不让推测,我就偏偏要去推测!我反手就把NestedInteger这个结构给实现出来:

嗯,其实这个实现也不难嘛,写出来之后,我不禁翻出前文 学习数据结构和算法的框架思维,发现这玩意儿竟然……

这玩意儿不就是棵 N 叉树吗?叶子节点是Integer类型,其val字段非空;其他节点都是List<NestedInteger>类型,其val字段为空,但是list字段非空,装着孩子节点

比如说输入是[[1,1],2,[1,1]],其实就是如下树状结构:

Image

好的,刚才题目说什么来着?把一个NestedInteger扁平化对吧?这不就等价于遍历一棵 N 叉树的所有「叶子节点」吗?我把所有叶子节点都拿出来,不就可以作为迭代器进行遍历了吗?

N 叉树的遍历怎么整?我又不禁翻出前文 学习数据结构和算法的框架思维 找出框架:

这个框架可以遍历所有节点,而我们只对整数型的NestedInteger感兴趣,也就是我们只想要「叶子节点」,所以traverse函数只要在到达叶子节点的时候把val加入结果列表即可:

这样,我们就把原问题巧妙转化成了一个 N 叉树的遍历问题,并且得到了解法。

Version 2: 进阶思路

以上解法虽然可以通过,但是在面试中,也许是有瑕疵的。

我们的解法中,一次性算出了所有叶子节点的值,全部装到result列表,也就是内存中,nexthasNext方法只是在对result列表做迭代。如果输入的规模非常大,构造函数中的计算就会很慢,而且很占用内存。

一般的迭代器求值应该是「惰性的」,也就是说,如果你要一个结果,我就算一个(或是一小部分)结果出来,而不是一次把所有结果都算出来。

如果想做到这一点,使用递归函数进行 DFS 遍历肯定是不行的,而且我们其实只关心「叶子节点」,所以传统的 BFS 算法也不行。实际的思路很简单:

调用hasNext时,如果nestedList的第一个元素是列表类型,则不断展开这个元素,直到第一个元素是整数类型

由于调用next方法之前一定会调用hasNext方法,这就可以保证每次调用next方法的时候第一个元素是整数型,直接返回并删除第一个元素即可。

看一下代码:

以这种方法,符合迭代器惰性求值的特性,是比较好的解法,建议拿小本本记下来!

Last updated

Was this helpful?