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.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
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 ;
}
// (row, col)用过了
if ( col < matrix [ row ] . length - 1 ) {
col + + ;
} else {
col = 0 ;
do {
row + + ;
} while ( row < matrix . length & & matrix [ row ] . length = = 0 ) ;
}
// 新的(row, col)
if ( row ! = matrix . length ) {
curUse = false ;
return true ;
} else {
return false ;
}
}
}
}