Open Shawngbk opened 7 years ago
public class Solution { public void setZeroes(int[][] matrix) { if(matrix == null) return; Set rowset = new HashSet(); Set colset = new HashSet(); //行宽 int rowlen = matrix.length; //列长 int collen = matrix[0].length; for(int i = 0; i < rowlen; i++) { for(int j = 0; j < collen; j++) { if(matrix[i][j] == 0) { rowset.add(i); colset.add(j); } } } for(int row : rowset) { for(int i = 0; i < collen; i++) { matrix[row][i] = 0; } } for(int col : colset) { for(int i = 0; i < rowlen; i++) { matrix[i][col] = 0; } } } }
public class Solution { public void setZeroes(int[][] matrix) { if(matrix == null) return; Set rowset = new HashSet();
Set colset = new HashSet();
//行宽
int rowlen = matrix.length;
//列长
int collen = matrix[0].length;
for(int i = 0; i < rowlen; i++) {
for(int j = 0; j < collen; j++) {
if(matrix[i][j] == 0) {
rowset.add(i);
colset.add(j);
}
}
}
for(int row : rowset) {
for(int i = 0; i < collen; i++) {
matrix[row][i] = 0;
}
}
for(int col : colset) {
for(int i = 0; i < rowlen; i++) {
matrix[i][col] = 0;
}
}
}
}