使用2个标记数组记录哪一行哪一列是否有0出现。先遍历一次。
class Solution{
public void setZeros(int[][] arr){
boolean[] row = new boolean[arr.length];
boolean[] col = new boolean[arr[0].length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] == 0){
row[i] = true;
col[j] = true;
}
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (row[i] || col[j]){
arr[i][j] = 0;
}
}
}
}
}