将0元素的对应行列置0,常规做法,faster than 74%
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function(matrix) {
var row = []
var col = []
for(var i = 0; i < matrix.length; i++){
for(var j = 0; j < matrix[0].length; j++){
if(matrix[i][j] === 0){
row[i] = 1
col[j] = 1
}
}
}
for(var i = 0; i < matrix.length; i++){
if(row[i] === 1) matrix[i].fill(0)
}
for(var i = 0; i < matrix[0].length; i++){
if(col[i] === 1){
for(var j = 0; j < matrix.length; j++)
matrix[j][i] = 0
}
}
return matrix
};