LeetCode341-扁平化嵌套列表迭代器

题目链接

英文链接:https://leetcode.com/problems/flatten-nested-list-iterator/

中文链接:https://leetcode-cn.com/problems/flatten-nested-list-iterator/

题目详述

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

列表中的项或者为一个整数,或者是另一个列表。

示例 1:

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

示例 2:

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

题目详解

有两种解决方法:

  • 在初始化的时候一次性递归读出所有数据存在列表中,迭代的过程就变成了迭代列表。
  • 不一次性读入所有数据,而是运用栈模拟迭代的过程(类似于 LeetCode589-N叉树的前序遍历,倒序 push 再弹出)。

一次性递归读入数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class LeetCode_00341 {

public class NestedIterator implements Iterator<Integer> {
private List<Integer> list;
private int index;

public NestedIterator(List<NestedInteger> nestedList) {
list = new ArrayList<>();
index = 0;
dfs(nestedList);
}

private void dfs(List<NestedInteger> nestedList) {
for (NestedInteger x : nestedList) {
if (x.isInteger()) {
list.add(x.getInteger());
} else {
dfs(x.getList());
}
}
}

@Override
public Integer next() {
return list.get(index++);
}

@Override
public boolean hasNext() {
return index < list.size();
}
}

public interface NestedInteger {
// @return true if this NestedInteger holds a single integer, rather than a nested list.
public boolean isInteger();

// @return the single integer that this NestedInteger holds, if it holds a single integer
// Return null if this NestedInteger holds a nested list
public Integer getInteger();

// @return the nested list that this NestedInteger holds, if it holds a nested list
// Return null if this NestedInteger holds a single integer
public List<NestedInteger> getList();
}
}

运用栈模拟迭代过程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class LeetCode_00341 {

public class NestedIterator implements Iterator<Integer> {
private Deque<NestedInteger> stack;

public NestedIterator(List<NestedInteger> nestedList) {
stack = new ArrayDeque<>();
for (int i = nestedList.size() - 1; i >= 0; --i) {
stack.push(nestedList.get(i));
}
}

@Override
public Integer next() {
return stack.pop().getInteger();
}

@Override
public boolean hasNext() {
while (!stack.isEmpty()) {
NestedInteger peek = stack.peek();
if (peek.isInteger()) {
return true;
}
List<NestedInteger> list = stack.pop().getList();
for (int i = list.size() - 1; i >= 0; --i) {
stack.push(list.get(i));
}
}
return false;
}
}

public interface NestedInteger {
// @return true if this NestedInteger holds a single integer, rather than a nested list.
public boolean isInteger();

// @return the single integer that this NestedInteger holds, if it holds a single integer
// Return null if this NestedInteger holds a nested list
public Integer getInteger();

// @return the nested list that this NestedInteger holds, if it holds a nested list
// Return null if this NestedInteger holds a single integer
public List<NestedInteger> getList();
}
}