题目链接:
LeetCode 73 — Set Matrix Zeroes
AC代码:
public class Problem73 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void setZeroes(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
HashSet<Integer> zeroRow = new HashSet<>();
HashSet<Integer> zeroCol = new HashSet<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
zeroRow.add(i);
zeroCol.add(j);
}
}
}
for (int i = 0; i < m; i++) {
if (zeroRow.contains(i)) {
for (int j = 0; j < n; j++) matrix[i][j] = 0;
}
}
for (int j = 0; j < n; j++) {
if (zeroCol.contains(j)) {
for (int i = 0; i < m; i++) matrix[i][j] = 0;
}
}
return;
}
}