73. Set Matrix Zeroes
Leetcode
題目
解答
var setZeroes = function(matrix) {
const m = matrix.length;
const n = matrix[0].length;
const rows = [];
const cols = [];
for(let i=0; i<m; i++) {
for(let j=0; j<n; j++) {
if(matrix[i][j] === 0) {
rows.push(i);
cols.push(j);
}
}
}
for(let i=0; i<rows.length; i++) {
const row = rows[i];
for(let j=0; j<n; j++) {
matrix[row][j] = 0;
}
}
for(let j=0; j<cols.length; j++) {
const col = cols[j];
for(let i=0; i<m; i++) {
matrix[i][col] = 0;
}
}
};測資
Last updated