Set Matrix Zeroes
Given an m x n integer matrix, if an element is 0, set its entire row and column to 0. You must do it in place — using the first row and first column of the matrix itself as marker flags avoids needing extra memory for the whole matrix.
Constraints
- m == matrix.length
- n == matrix0.length
- 1 ≤ m, n ≤ 200
- -231 ≤ matrixi[j] ≤ 231 - 1
Example
matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]][[1, 0, 1], [0, 0, 0], [1, 0, 1]]Explanation The single 0 at (1, 1) zeroes out all of row 1 and all of column 1.
In plain terms
- Marker flags
- A cell reused to record a fact ("this row/column needs zeroing") instead of allocating a brand-new array to hold that same information.
Mark rows and columns to zero, then apply the marks
Check the first row and first column for zeros before reusing them as markers: neither has one.
What happens in this step
firstRowHasZero = false firstColHasZero = false Neither the first row [1,1,1] nor the first column [1,1,1] contains a 0, so both flags stay false.
Steps to visualize
- First check whether the first row or first column itself already contains a zero, and remember that separately — they're about to be reused as markers.
- Scan the rest of the matrix: whenever a cell is 0, mark its row in the first column and its column in the first row.
- Scan again: zero out any cell whose row-marker or column-marker is set.
- Finally, if the first row or first column originally had a zero, zero the whole row or column now.
Walk through the code
Same walkthrough, now with the code. Press Next to move one step and watch which lines run.
Check the first row and first column for zeros before reusing them as markers: neither has one.
What happens in this step
firstRowHasZero = false firstColHasZero = false Neither the first row [1,1,1] nor the first column [1,1,1] contains a 0, so both flags stay false.
Solution
function setZeroes(matrix) {
const rows = matrix.length;
const cols = matrix[0].length;
let firstRowHasZero = false;
let firstColHasZero = false;
for (let col = 0; col < cols; col++) {
if (matrix[0][col] === 0) {
firstRowHasZero = true;
}
}
for (let row = 0; row < rows; row++) {
if (matrix[row][0] === 0) {
firstColHasZero = true;
}
}
for (let row = 1; row < rows; row++) {
for (let col = 1; col < cols; col++) {
if (matrix[row][col] === 0) {
matrix[row][0] = 0;
matrix[0][col] = 0;
}
}
}
for (let row = 1; row < rows; row++) {
for (let col = 1; col < cols; col++) {
if (matrix[row][0] === 0 || matrix[0][col] === 0) {
matrix[row][col] = 0;
}
}
}
if (firstRowHasZero) {
for (let col = 0; col < cols; col++) {
matrix[0][col] = 0;
}
}
if (firstColHasZero) {
for (let row = 0; row < rows; row++) {
matrix[row][0] = 0;
}
}
return matrix;
}- Time
- O(m * n)
- Space
- O(1)
Test cases
| Input | Expected | Covers |
|---|---|---|
matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]] | [[1, 0, 1], [0, 0, 0], [1, 0, 1]] | example from the docstring |
matrix = [[1]] | [[1]] | smallest valid input with no zero present |
matrix = [[0]] | [[0]] | smallest valid input that is already zero |
matrix = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] | [[0, 0, 0], [0, 4, 5], [0, 7, 8]] | the zero sits in both the first row and first column, the markers themselves |
matrix = [[0, 0], [0, 0]] | [[0, 0], [0, 0]] | a matrix that is already all zeroes |
matrix = [[1, 2], [3, 4]] | [[1, 2], [3, 4]] | no zero anywhere, so nothing changes |