这次直接上代码吧
class Solution {
public void setZeroes(int[][] matrix) {
int m = matrix.length;
if (m == 0) {
return;
}
int n = matrix[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
matrix[i][j] = -1;
for (int k = 0; k < m; k++) {
if (matrix[k][j] != 0) matrix[k][j] = Integer.MIN_VALUE / 2;
}
for (int k = 0; k < n; k++) {
if (matrix[i][k] != 0) matrix[i][k] = Integer.MIN_VALUE / 2;
}
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == Integer.MIN_VALUE / 2) {
matrix[i][j] = 0;
}
}
}
}
}
其实我做的不严谨,正确做法应该是先统计出来0出现的位置,然后再进行操作。但是我懒得改了