class Solution {
public void setZeroes(int[][] matrix) {
int rows = matrix.length;
int columns = matrix[0].length;
HashMap<Integer, Integer> map1 = new HashMap<>();
HashMap<Integer, Integer> map2 = new HashMap<>();
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
if (matrix[i][j] == 0) {
map1.put(i, map1.getOrDefault(i, 0) + 1);
map2.put(j, map2.getOrDefault(j, 0) + 1);
}
}
}
for (int i = 0; i < rows; ++i) {
if (map1.containsKey(i)) {
for (int j = 0; j < columns; ++j) {
matrix[i][j] = 0;
}
}
}
for (int j = 0; j < columns; ++j) {
if (map2.containsKey(j)) {
for (int i = 0; i < rows; ++i) {
matrix[i][j] = 0;
}
}
}
}
}
LeetCode——数组和字符串——零矩阵(java解法)
最新推荐文章于 2024-08-17 20:25:49 发布