Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
AC代码,虽然有点复杂,但是一次通过了。
public class Solution {
public void setZeroes(int[][] matrix) {
if (matrix == null)
return;
int row[] = new int[matrix.length];
int col[] = new int[matrix[0].length];
if(matrix.length == 0 || matrix[0].length == 0)
return;
for(int i = 0; i < matrix.length; i ++)
for(int j = 0; j < matrix[0].length; j ++){
if(matrix[i][j] == 0){
row[i] = 1;
col[j] = 1;
}
}
for(int i = 0; i < matrix.length; i ++)
{
if(row[i] == 1){
for(int j = 0; j < matrix[0].length; j ++){
matrix[i][j] = 0;
}
}
}//end for
for(int i = 0; i < matrix[0].length; i ++)
{
if(col[i] == 1){
for(int j = 0; j < matrix.length; j ++){
matrix[j][i] = 0;
}
}
}//end for
}
}