你不需要知道具体哪个位置有0,你只用标记有0的行/列,然后把这些都变成0即可
java
class Solution {
public void setZeroes(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
boolean[] row = new boolean[m];
boolean[] col = new boolean[n];
for(int i = 0; i < m; i ++){
for(int j = 0; j< n; j++){
if(matrix[i][j] == 0){
row[i] = col[j] = true;
}
}
}
for(int i = 0; i < m; i ++){
for(int j = 0; j< n; j++){
if(row[i] || col[j]){
matrix[i][j] = 0;
}
}
}
}
}