-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlattenNestedListIterator.java
More file actions
143 lines (117 loc) · 3.83 KB
/
FlattenNestedListIterator.java
File metadata and controls
143 lines (117 loc) · 3.83 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package medium;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import util.NestedInteger;
/**
*
* ClassName: FlattenNestedListIterator
* @author chenyiAlone
* Create Time: 2019/02/17 21:35:28
* Description: No.341
*
* 思路:
* 一、
* 1. 使用栈来完成,栈用于记录当前层的下一个位置,用于从下一层的退出
* 2. toNext()
* 1). 先进行判断是否需要向外跳
* 2). 跳完以后还到达了末尾,证明已经到了最外层,直接退出
* 3). 当前的为 List 就进入 List
* 4). 如果新进入的 List 为空,就再往外跳
*
* 二、
* 直接遍历保存到 List 中
*
*
* 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 1:
Input: [[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: [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].
*/
public class FlattenNestedListIterator implements Iterator<Integer> {
private int curP;
private List<NestedInteger> curList;
private Stack<Pair> stack = new Stack<>();
public FlattenNestedListIterator(List<NestedInteger> nestedList) {
curList = nestedList;
curP = 0;
toNext();
}
private void outList() {
while (curP >= curList.size()) {
if (stack.isEmpty()) break;
Pair pair = stack.pop();
curP = pair.p;
curList = pair.list; // 向外层跳
}
}
private void toNext() {
outList();
if (curP >= curList.size()) return;
NestedInteger n = curList.get(curP);
while (n != null && !n.isInteger()) {
stack.push(new Pair(curP + 1, curList));
curP = 0;
curList = n.getList();
if (curList.size() == 0) outList();
n = curList.size() > curP ? curList.get(curP) : null;
}
}
@Override
public Integer next() {
int ret = curList.get(curP++).getInteger();
toNext();
return ret;
}
@Override
public boolean hasNext() {
return curP < curList.size();
}
private class Pair {
private int p;
private List<NestedInteger> list;
private Pair(int p, List<NestedInteger> list) {
this.p = p;
this.list = list;
}
}
/*
* 1.使用 List 来直接保存所有的结果
*
*/
private class Old {
LinkedList<Integer> res = new LinkedList<>();
public Old(List<NestedInteger> nestedList) {
helper(res, nestedList);
}
public Integer next() {
return res.pop();
}
public boolean hasNext() {
return !res.isEmpty();
}
private void helper(List<Integer> res, List<NestedInteger> nestedList) {
for (NestedInteger nestedInteger : nestedList) {
if (nestedInteger.isInteger()) {
res.add(nestedInteger.getInteger());
} else {
helper(res, nestedInteger.getList());
}
}
}
}
}
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/