# 刷java算法题出现了空指针错误

0
``````public class Solution {
public void setZeroes(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
Boolean[] rows = new Boolean[m];
Boolean[] cols = new Boolean[n];

for(int i = 0; i< m; i++){
for(int j = 0; j< n; j++){
if(matrix[i][j] == 0){
rows[i] = true;
cols[j] = true;
}
}
}
for(int i = 0; i<m; i++){
for(int j = 0; j< n; j++){
if(rows[i] ==true || cols[j] == true)
matrix[i][j] = 0;
}

}
}
}``````

## 2个回答

0

0

``````/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function(matrix) {
const rowCount = matrix.length;
const colCount = rowCount && matrix[0].length;

const rowMarks = [];
const colMarks = [];

for (let i = 0; i < rowCount; i++) {
for (let j = 0; j < colCount; j++) {
if (matrix[i][j] === 0) {
rowMarks[i] = true;
colMarks[j] = true;
}
}
}

for (let i = 0; i < rowMarks.length; i++) {
if (!rowMarks[i]) {
continue;
}

for (let j = 0; j < colCount; j++) {
matrix[i][j] = 0;
}
}

for (let j = 0; j < colMarks.length; j++) {
if (!colMarks[j]) {
continue;
}

for (let i = 0; i < rowCount; i++) {
matrix[i][j] = 0;
}
}
};``````
1