-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathSolution.java
64 lines (49 loc) · 1.34 KB
/
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
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
/**
* @author Oleg Cherednik
* @since 01.04.2019
*/
public class Solution {
public static void main(String... args) {
int[][] data = {
{ 1, 2 },
{ 3 },
{},
{ 4, 5, 6 } };
Iterator2D it = new Iterator2D(data);
for (int i = 0; i < 7; i++)
System.out.println(it.next());
}
public static final class Iterator2D {
private final int[][] data;
private int row = 0;
private int col = -1;
private boolean used = true;
public Iterator2D(int[][] data) {
this.data = data;
}
public int next() {
if (!findNextElement())
throw new RuntimeException("No more elements");
used = true;
return data[row][col];
}
public boolean has_next() {
return findNextElement();
}
private boolean findNextElement() {
if (row >= data.length)
return false;
if (!used)
return true;
used = false;
while (row < data.length) {
col++;
if (col < data[row].length)
return true;
col = -1;
row++;
}
return false;
}
}
}