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 class009 ;
public class Code01_SetMatrixZeroes {
/*
* 评测代码可以直接去leetcode搜索: Set Matrix Zeroes
*
*/
public static void setZeroes1 ( int [ ] [ ] matrix ) {
boolean row0Zero = false ;
boolean col0Zero = false ;
int i = 0 ;
int j = 0 ;
for ( i = 0 ; i < matrix [ 0 ] . length ; i + + ) {
if ( matrix [ 0 ] [ i ] = = 0 ) {
row0Zero = true ;
break ;
}
}
for ( i = 0 ; i < matrix . length ; i + + ) {
if ( matrix [ i ] [ 0 ] = = 0 ) {
col0Zero = true ;
break ;
}
}
// 跳过了所有0行、0列的数据
for ( i = 1 ; i < matrix . length ; i + + ) {
for ( j = 1 ; j < matrix [ 0 ] . length ; j + + ) {
if ( matrix [ i ] [ j ] = = 0 ) {
matrix [ i ] [ 0 ] = 0 ;
matrix [ 0 ] [ j ] = 0 ;
}
}
}
for ( i = 1 ; i < matrix . length ; i + + ) {
for ( j = 1 ; j < matrix [ 0 ] . length ; j + + ) {
if ( matrix [ i ] [ 0 ] = = 0 | | matrix [ 0 ] [ j ] = = 0 ) {
matrix [ i ] [ j ] = 0 ;
}
}
}
if ( row0Zero ) {
for ( i = 0 ; i < matrix [ 0 ] . length ; i + + ) {
matrix [ 0 ] [ i ] = 0 ;
}
}
if ( col0Zero ) {
for ( i = 0 ; i < matrix . length ; i + + ) {
matrix [ i ] [ 0 ] = 0 ;
}
}
}
public static void setZeroes2 ( int [ ] [ ] matrix ) {
boolean col0 = false ;
int i = 0 ;
int j = 0 ;
for ( i = 0 ; i < matrix . length ; i + + ) {
for ( j = 0 ; j < matrix [ 0 ] . length ; j + + ) {
if ( matrix [ i ] [ j ] = = 0 ) {
matrix [ i ] [ 0 ] = 0 ;
if ( j = = 0 ) {
col0 = true ;
} else {
matrix [ 0 ] [ j ] = 0 ;
}
}
}
}
for ( i = matrix . length - 1 ; i > = 0 ; i - - ) {
for ( j = 1 ; j < matrix [ 0 ] . length ; j + + ) {
if ( matrix [ i ] [ 0 ] = = 0 | | matrix [ 0 ] [ j ] = = 0 ) {
matrix [ i ] [ j ] = 0 ;
}
}
}
if ( col0 ) {
for ( i = 0 ; i < matrix . length ; i + + ) {
matrix [ i ] [ 0 ] = 0 ;
}
}
}
}