一、题目地址
https://leetcode-cn.com/problems/set-matrix-zeroes/
二、思路地址
三、具体代码
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function(matrix) {
let firstRowHasZero = false;
let firstColHasZero = false;
// 检查第一列中是否含有0
for(let i=0; i<matrix.length; i++) {
if(matrix[i][0] === 0) {
firstColHasZero = true;
}
}
// 检查第一行中是否含有0
for(let i=0; i<matrix[0].length; i++) {
if(matrix[0][i] === 0) {
firstRowHasZero = true;
}
}
// 使用第一行和第一列,来标记其余行列是否含有0
for(let row=1; row<matrix.length; row++) {
for(let col=1; col<matrix[0].length; col++) {
if(matrix[row][col] === 0) {
matrix[0][col] = 0;
matrix[row][0] = 0;
}
}
}
// 接下来,利用第一行和第一列的标0情况,将matrix中的数字标0
for(let row=1; row<matrix.length; row++) {
for(let col=1; col<matrix[0].length; col++) {
if(matrix[0][col] === 0 || matrix[row][0] === 0) {
matrix[row][col] = 0;
}
}
}
// 最后,处理第一行和第一列
// 如果firstRowHasZero等于0,,则把第一行都设置为0
if(firstRowHasZero === true) {
for(let col=0; col<matrix[0].length; col++) {
matrix[0][col] = 0;
}
}
// 如果firstColHasZero等于0,,则把第一列都设置为0
if(firstColHasZero === true) {
for(let row=0; row<matrix.length; row++) {
matrix[row][0] = 0;
}
}
return matrix;
};