问题:
class Solution { public void setZeroes(int[][] matrix) { boolean[] row = new boolean[matrix.length]; boolean[] column = new boolean[matrix[0].length]; for(int i = 0;i<matrix.length;i++) { for(int j = 0;j<matrix[0].length;j++) { if(matrix[i][j] == 0) { row[i] = true; column[j] = true; } } } for(int i=0;i<matrix.length;i++) { for(int j=0;j<matrix[0].length;j++) { if(row[i]||column[j]) { matrix[i][j] = 0; } } } } }
定义boolean型数组row,column。遍历二维数组,将值为0的项的行和列赋值给布尔数组。再次遍历数组,只要行值或列值有一个为true,那么就将具体值matrix[i][j]置0;
原文:https://www.cnblogs.com/HanLue/p/14794526.html