题目
法1:自己想的笨蛋方法
class Solution {public void setZeroes(int[][] matrix) {Set<Integer> rowSet = new HashSet<>();Set<Integer> columnSet = new HashSet<>();for (int i = 0; i < matrix.length; ++i) {for (int j = 0; j < matrix[0].length; ++j) {if (matrix[i][j] == 0) {rowSet.add(i);columnSet.add(j);}}}for (int i = 0; i < matrix.length; ++i) {if (rowSet.contains(i)) {Arrays.fill(matrix[i], 0);}}for (int j = 0; j < matrix[0].length; ++j) {if (columnSet.contains(j)) {for (int i = 0; i < matrix.length; ++i) {matrix[i][j] = 0;}}}}
}// 代码优雅版
class Solution {public void setZeroes(int[][] matrix) {int m = matrix.length, 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;}}}}
}作者:力扣官方题解
链接:https://leetcode.cn/problems/set-matrix-zeroes/solutions/669901/ju-zhen-zhi-ling-by-leetcode-solution-9ll7/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
法2:
放个链接,估计真遇到了也写不出来。。。
https://leetcode.cn/problems/set-matrix-zeroes/solutions/670278/xiang-jie-fen-san-bu-de-o1-kong-jian-jie-dbxd/?envType=study-plan-v2&envId=top-100-liked