-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-341-Flatten-Nested-List-Iterator.java
164 lines (139 loc) · 4.73 KB
/
LeetCode-341-Flatten-Nested-List-Iterator.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Time: O(N)
Space: O(N)
*/
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* 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();
* }
*/
// public class NestedIterator implements Iterator<Integer> {
// Queue<Integer> queue;
// public NestedIterator(List<NestedInteger> nestedList) {
// queue = new LinkedList<>();
// recursive(nestedList, queue);
// }
// private void recursive(List<NestedInteger> list, Queue<Integer> queue) {
// for (NestedInteger ni : list) {
// if (ni.isInteger()) {
// queue.offer(ni.getInteger());
// } else {
// recursive(ni.getList(), queue);
// }
// }
// }
// @Override
// public Integer next() {
// if (!queue.isEmpty()) {
// return queue.poll();
// }
// return -1;
// }
// @Override
// public boolean hasNext() {
// return !queue.isEmpty();
// }
// }
// 1. Load when initializing
// public class NestedIterator implements Iterator<Integer> {
// Queue<Integer> queue;
// public NestedIterator(List<NestedInteger> nestedList) {
// queue = new LinkedList<>();
// enqueue(nestedList, queue);
// }
// private void enqueue(List<NestedInteger> nestedList, Queue<Integer> queue) {
// for (NestedInteger ni : nestedList) {
// if (ni.isInteger()) {
// queue.offer(ni.getInteger());
// } else {
// enqueue(ni.getList(), queue);
// }
// }
// }
// @Override
// public Integer next() {
// return queue.poll();
// }
// @Override
// public boolean hasNext() {
// return !queue.isEmpty();
// }
// }
// 2. Lazy Load
/*
There are 1000000000 Integers in nestedList and the user only calls next() once. -> You have to put 1000000000 Integers into stack, but the user only takes the 1st one. Instead, we should push to stack "lazy".
*/
// public class NestedIterator implements Iterator<Integer> {
// Stack<NestedInteger> stack;
// public NestedIterator(List<NestedInteger> nestedList) {
// stack = new Stack<>();
// recursive(nestedList, stack);
// }
// @Override
// public Integer next() {
// if (!hasNext()) throw new java.util.NoSuchElementException();
// return stack.pop().getInteger();
// }
// @Override
// public boolean hasNext() {
// while (!stack.isEmpty() && !stack.peek().isInteger()) {
// recursive(stack.pop().getList(), stack);
// }
// return !stack.isEmpty();
// }
// private void recursive(List<NestedInteger> list, Stack<NestedInteger> stack) {
// for (int i = list.size() - 1; i >= 0; i--) {
// stack.push(list.get(i));
// }
// }
// }
// 2. Another implementation of Lazy Loading. Lazy loading when calling `hasNext()`
public class NestedIterator implements Iterator<Integer> {
Queue<Integer> queue;
List<NestedInteger> nestedList;
public NestedIterator(List<NestedInteger> nestedList) {
this.queue = new LinkedList<>();
this.nestedList = nestedList;
}
@Override
public Integer next() {
return queue.poll();
}
@Override
public boolean hasNext() {
if (queue.isEmpty() && nestedList.isEmpty()) return false;
while (queue.isEmpty() && nestedList.size() != 0) {
NestedInteger ni = nestedList.remove(0);
List<NestedInteger> list = new LinkedList<>();
list.add(ni);
enqueue(list, queue);
}
return !queue.isEmpty();
}
private void enqueue(List<NestedInteger> nestedList, Queue<Integer> queue) {
for (NestedInteger ni : nestedList) {
if (ni.isInteger()) {
queue.offer(ni.getInteger());
} else {
enqueue(ni.getList(), queue);
}
}
}
}
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/