-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlatten2DVector.java
54 lines (44 loc) · 1.3 KB
/
Flatten2DVector.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.List;
/**
* LeetCode
* Flatten 2D Vector
* https://leetcode.com/problems/flatten-2d-vector/
* http://buttercola.blogspot.com/2015/08/leetcode-flatten-2d-vector.html
* https://aaronice.gitbook.io/lintcode/data_structure/flatten-2d-vector
* #Medium
*/
@SuppressWarnings("unused")
public class Flatten2DVector {
static class Vector2D {
private final List<List<Integer>> vec2d;
private int rowId;
private int colId;
private final int numRows;
public Vector2D(List<List<Integer>> vec2d) {
this.vec2d = vec2d;
rowId = 0;
colId = 0;
numRows = vec2d.size();
}
public int next() {
int ans = 0;
if (colId < vec2d.get(rowId).size()) {
ans = vec2d.get(rowId).get(colId);
}
colId++;
if (colId == vec2d.get(rowId).size()) {
colId = 0;
rowId++;
}
return ans;
}
public boolean hasNext() {
while (rowId < numRows && (vec2d.get(rowId) == null || vec2d.get(rowId).isEmpty())) {
rowId++;
}
return vec2d != null &&
!vec2d.isEmpty() &&
rowId < numRows;
}
}
}