You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
850 B

2 years ago
package class33;
public class Problem_0251_Flatten2DVector {
public static class Vector2D {
private int[][] matrix;
private int row;
private int col;
private boolean curUse;
public Vector2D(int[][] v) {
matrix = v;
row = 0;
col = -1;
curUse = true;
hasNext();
}
public int next() {
int ans = matrix[row][col];
curUse = true;
hasNext();
return ans;
}
public boolean hasNext() {
if (row == matrix.length) {
return false;
}
if (!curUse) {
return true;
}
// (rowcol)用过了
if (col < matrix[row].length - 1) {
col++;
} else {
col = 0;
do {
row++;
} while (row < matrix.length && matrix[row].length == 0);
}
// 新的(rowcol)
if (row != matrix.length) {
curUse = false;
return true;
} else {
return false;
}
}
}
}