作者:disappearedgod
时间:2014-9-13
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) {
if(matrix.length == 0)
return;
if(matrix[0].length == 0)
return;
int max = matrix[0].length * matrix.length;
Set line = new HashSet();
Set row= new HashSet();
for(int i = 0 ; i < matrix.length; i++){
for(int j = 0; j < matrix[0].length; j++){
if(matrix[i][j] == 0){
row.add(i);
line.add(j);
}
}
}
boolean rowSet = false;
for(int i = 0; i<matrix.length; i++){
if(row.contains(i))
rowSet = true;
else
rowSet = false;
for(int j = 0; j < matrix[0].length; j++){
if(rowSet || line.contains(j))
matrix[i][j] = 0;
}
}
}
}