-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.java
30 lines (26 loc) · 890 Bytes
/
Solution.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
import java.util.*;
public class NestedIterator implements Iterator<Integer> {
private Stack<NestedInteger> stack;
public NestedIterator(List<NestedInteger> nestedList) {
this.stack = new Stack<>();
for (int i = nestedList.size() - 1; i >= 0; i--) {
this.stack.push(nestedList.get(i));
}
}
@Override
public Integer next() {
return stack.pop().getInteger();
}
@Override
public boolean hasNext() {
while (!stack.empty()) {
NestedInteger cur = stack.peek();
if (cur.isInteger()) return true;
stack.pop();
for (int i = cur.getList().size() - 1; i >= 0; i--) {
stack.push(cur.getList().get(i));
}
}
return false;
}
}