-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lc341.java
52 lines (42 loc) · 1.36 KB
/
Lc341.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
package leetcode;
import java.util.*;
/**
* @author Kuma
* @date 2021年3月23日
* 341. 扁平化嵌套列表迭代器
*/
public class Lc341 {
}
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();
}
class NestedIterator implements Iterator<Integer> {
Stack<NestedInteger> st = new Stack<>();
public NestedIterator(List<NestedInteger> nestedList) {
Collections.reverse(nestedList);
for (NestedInteger i : nestedList){
st.push(i);
}
}
@Override
public Integer next() {
return st.pop().getInteger();
}
@Override
public boolean hasNext() {
while (!st.isEmpty() && !st.peek().isInteger()){
Collections.reverse(st.peek().getList());
for (NestedInteger i : st.pop().getList()){
st.push(i);
}
}
return !st.isEmpty() && st.peek().isInteger();
}
}