Set Matrix Zeroes
Medium · Matrices
Jump to codeIf any entry in a rectangular matrix is zero, make its entire row and column zero. Perform the update in place using constant auxiliary space.
Examples
Example 1
matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
Example 2
matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
Constraints
- 1 <= matrix.length, matrix[0].length <= 100
- -2^31 <= matrix[i][j] <= (2^31) - 1
Function contract
Modify matrix in place. The grader checks its value after your function returns.
Need a hint?
The first row and column can store markers, but their original zero status must be saved.
Real matrix interview questions
Questions on the same topic that candidates reported from real company interviews. Some company questions are locked and open with Premium or XP.
- Count Rectangle Coverage on a GridGoogle
- Minimize Delivery-Grid InconvenienceAmazon
- Compute time to infect all cellsOpenAI
Solution approach
Record whether the first row and first column originally contain a zero. Scan the interior and mark affected rows/columns in those first edges. Zero the interior from the markers, then update the first row and column using their saved flags.
Complexity
O(mn) time and O(1) auxiliary space.
Python reference solution
def setZeroes(matrix):
if not matrix or not matrix[0]:
return
rows, cols = len(matrix), len(matrix[0])
first_row = any(value == 0 for value in matrix[0])
first_col = any(row[0] == 0 for row in matrix)
for row in range(1, rows):
for col in range(1, cols):
if matrix[row][col] == 0:
matrix[row][0] = matrix[0][col] = 0
for row in range(1, rows):
for col in range(1, cols):
if matrix[row][0] == 0 or matrix[0][col] == 0:
matrix[row][col] = 0
if first_row:
for col in range(cols):
matrix[0][col] = 0
if first_col:
for row in range(rows):
matrix[row][0] = 0
JavaScript solution
function setZeroes(matrix) {
if (!matrix.length || !matrix[0].length) return;
const rows = matrix.length, cols = matrix[0].length;
const firstRow = matrix[0].some(value => value === 0), firstCol = matrix.some(row => row[0] === 0);
for (let row = 1; row < rows; row++) {
for (let col = 1; col < cols; col++) if (matrix[row][col] === 0) matrix[row][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 (firstRow) matrix[0].fill(0);
if (firstCol) for (const row of matrix) row[0] = 0;
}
Java solution
import java.util.*;
class Solution {
public void setZeroes(int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) return;
int rows = matrix.length, cols = matrix[0].length;
boolean firstRow = false, firstCol = false;
for (int col = 0; col < cols; col++) if (matrix[0][col] == 0) firstRow = true;
for (int row = 0; row < rows; row++) if (matrix[row][0] == 0) firstCol = true;
for (int row = 1; row < rows; row++) {
for (int col = 1; col < cols; col++) if (matrix[row][col] == 0) matrix[row][0] = matrix[0][col] = 0;
}
for (int row = 1; row < rows; row++) {
for (int col = 1; col < cols; col++) {
if (matrix[row][0] == 0 || matrix[0][col] == 0) matrix[row][col] = 0;
}
}
if (firstRow) Arrays.fill(matrix[0], 0);
if (firstCol) for (int[] row : matrix) row[0] = 0;
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void setZeroes(vector<vector<int>> &matrix) {
if (matrix.empty() || matrix[0].empty()) return;
int rows = matrix.size(), cols = matrix[0].size();
bool firstRow = false, firstCol = false;
for (int col = 0; col < cols; col++) if (matrix[0][col] == 0) firstRow = true;
for (int row = 0; row < rows; row++) if (matrix[row][0] == 0) firstCol = true;
for (int row = 1; row < rows; row++) {
for (int col = 1; col < cols; col++) if (matrix[row][col] == 0) matrix[row][0] = matrix[0][col] = 0;
}
for (int row = 1; row < rows; row++) {
for (int col = 1; col < cols; col++) {
if (matrix[row][0] == 0 || matrix[0][col] == 0) matrix[row][col] = 0;
}
}
if (firstRow) fill(matrix[0].begin(), matrix[0].end(), 0);
if (firstCol) for (auto& row : matrix) row[0] = 0;
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 8 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
[
1,
1,
1
],
[
1,
0,
1
],
[
1,
1,
1
]
]
][
[
1,
0,
1
],
[
0,
0,
0
],
[
1,
0,
1
]
][
[
[
0,
1,
2,
0
],
[
3,
4,
5,
2
],
[
1,
3,
1,
5
]
]
][
[
0,
0,
0,
0
],
[
0,
4,
5,
0
],
[
0,
3,
1,
0
]
][
[
[
1,
2
],
[
3,
4
]
]
][
[
1,
2
],
[
3,
4
]
][
[
[
0,
1
],
[
2,
3
]
]
][
[
0,
0
],
[
0,
3
]
][
[
[
1,
0,
3
],
[
4,
5,
6
],
[
0,
8,
9
]
]
][
[
0,
0,
0
],
[
0,
0,
6
],
[
0,
0,
0
]
]Drafts are saved per language in this browser when storage is available.