题目链接
零矩阵
题目描述
注意
- 无
解答思路
- 用另外两个数组分别保存对应的行和列是否出现了0,后续在填充元素时则根据这两个数组中对应的值确定是否填0
代码
class Solution {public void setZeroes(int[][] matrix) {int m = matrix.length;int 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;}}}}
}
关键点
- 使用boolean数组存储行和列中是否存在0,以空间换时间



















