Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
public class Solution {
public void setZeroes(int[][] matrix) {
int[] row=new int[matrix.length];
int[] col=new int[matrix[0].length]; // don't forget the default value is 0;
/*check which row or column is 0*/
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++)
for(int j=0;j<matrix[0].length;j++){
if(row[i]==1||col[j]==1)
matrix[i][j]=0; // this guarantees that the column and the rowis set to 0
}
}
}