Skip to content
Onsite

Set Matrix Zeroes

MediumP2Coding

Given an m x n integer matrix, if an element is 0, set its entire row and its entire column to 0. Modify the matrix in place; the function returns nothing.

Only zeros that were in the original matrix cause rows and columns to be cleared. Zeros created by the operation do not spread further.

Example 1

Input
matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output
[[1,0,1],[0,0,0],[1,0,1]]

Example 2

Input
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]]

Example 3

Input
matrix = [[1]]
Output
[[1]]

Constraints

  • 1 <= m, n <= 200
  • -2^31 <= matrix[i][j] <= 2^31 - 1

Hints

Run checks the 3 visible tests, Submit all 12.