Use the first row and first column as markers. Scan the matrix — when a zero is found, mark the corresponding first-row and first-column cell as zero. Then do a second pass to zero out marked rows and columns. Handle the first row and column separately using two dedicated flags to avoid overwriting markers prematurely.
function setZeroes(matrix) {
const m = matrix.length;
const n = matrix[0].length;
let firstRowZero = false;
let firstColZero = false;
for (let i = 0; i < m; i++) {
if (matrix[i][0] === 0) firstColZero = true;
}
for (let j = 0; j < n; j++) {
if (matrix[0][j] === 0) firstRowZero = true;
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][j] === 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
matrix[i][j] = 0;
}
}
}
if (firstRowZero) {
for (let j = 0; j < n; j++) matrix[0][j] = 0;
}
if (firstColZero) {
for (let i = 0; i < m; i++) matrix[i][0] = 0;
}
}Time: O(m * n) — two full passes over the matrix. Space: O(1) — markers stored in the matrix itself.
Loading discussion…
Saved in this browser only. Private to you.