Given an m x n integer matrix, if any element is 0, set its entire row and its entire column to 0. Do this in place.
Function Signature
def set_zeroes(matrix: list[list[int]]) -> None:
Rules
-
Modify
matrix
in place and return nothing. The result is the contents of
matrix
after the call.
-
Only zeros present in the input trigger a row and column reset. A cell that becomes 0 during the update does not clear its own row or column.
Constraints
-
m
is the number of rows and
n
is the number of columns.
-
1 <= m, n <= 250
-
-10^4 <= matrix[i][j] <= 10^4
Examples
Input: matrix = [[2,3,4],[5,0,6],[7,8,9]]
Output: [[2,0,4],[0,0,0],[7,0,9]]
The only zero is at row 1, column 1, so row 1 and column 1 become all zeros and the four corner values stay.
Input: matrix = [[0,5,7,1],[2,4,6,8],[3,9,0,2]]
Output: [[0,0,0,0],[0,4,0,8],[0,0,0,0]]
The zeros at (0, 0) and (2, 2) clear rows 0 and 2 and columns 0 and 2. In row 1, only the values in columns 1 and 3 survive.
Input: matrix = [[1,2],[0,4],[5,6]]
Output: [[0,2],[0,0],[0,6]]
The zero at (1, 0) clears row 1 and column 0. The 2 and the 6 sit outside both, so they stay.