-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path341.扁平化嵌套列表迭代器.java
54 lines (44 loc) · 1.32 KB
/
341.扁平化嵌套列表迭代器.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
import java.util.Iterator;
import java.util.List;
/*
* @lc app=leetcode.cn id=341 lang=java
*
* [341] 扁平化嵌套列表迭代器
*/
// @lc code=start
// This is the interface that allows for creating nested lists.
// You should not implement it, or speculate about its implementation
class NestedIterator implements Iterator<Integer> {
private List<NestedInteger> list;
public NestedIterator(List<NestedInteger> nestedList) {
this.list = nestedList;
}
@Override
public Integer next() {
return this.list.remove(0).getInteger();
}
@Override
public boolean hasNext() {
if (this.list.isEmpty())
return false;
NestedInteger first = this.list.get(0);
while (first != null && !first.isInteger()) {
first = this.list.remove(0);
List<NestedInteger> childs = first.getList();
if (!childs.isEmpty()) {
this.list.addAll(0, childs);
}
if (this.list.isEmpty()) {
return false;
}
first = this.list.get(0);
}
return !this.list.isEmpty();
}
}
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/
// @lc code=end