Problem
You get a matrix matrix of integers with m rows and n columns. A matrix is a grid of numbers. For each item with the value 0, set all the items in its row and its column to 0.
Use only the 0 values that the original matrix has. A 0 that you write does not set more rows or columns to 0.
Change the matrix in place. In place means that you change the given matrix and do not return a new matrix. The function returns nothing.
Example 1
- Input:
- matrix = [[1,2,3],[4,0,6],[7,8,9]]
- Output:
- [[1,0,3],[0,0,0],[7,0,9]]
- Explanation:
- The only 0 is at row 1, column 1. So all the items in row 1 and in column 1 become 0.
Example 2
- Input:
- matrix = [[5,0,2,0],[3,4,1,6],[0,7,8,9]]
- Output:
- [[0,0,0,0],[0,0,1,0],[0,0,0,0]]
- Explanation:
- The original 0 values are in rows 0 and 2, and in columns 0, 1 and 3. Only the item with the value 1 keeps its value.
Constraints
- m == matrix.length
- n == matrix[0].length
- 1 <= m, n <= 200
- -2^31 <= matrix[i][j] <= 2^31 - 1
Follow-up
A simple solution uses O(m * n) extra space, and a better solution uses O(m + n). Can you find a solution that uses O(1) extra space?
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- The first was leetcode Set Matrix Zeroes.
The LeetCode problem Set Matrix Zeroes is a classic 2D array manipulation problem. The description is as follows:
You are given an
𝑚
×
𝑛
m×n matrix of integers. If any element is 0, you must set its entire row and column to 0. In other words, for each cell that contains 0, all cells in the same row and column should be changed to 0.
Coding
- 01 Matrix
Coding
- 1. Count number of islands in matrix (Leetcode challenge)
Coding
- Array of Objects to Matrix
Coding
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 2 records
Meta
Leetcode 73. Set Matrix Zeroes
Reported May 25, 2026 · 2 source reports
MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View sourceMicrosoft
Leetcode 73. Set Matrix Zeroes
Reported Jan 1, 2020 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026
View source