Skip to content

Set Matrix Zeroes

MediumArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.

Given an m x n integer matrix, whenever a cell is 0, set its entire row and its entire column to 0. Do it in place. Only zeros that were in the original matrix cause this; zeros you write do not spread further. As a follow-up, use O(1) extra space.

[[1, 1, 1],          [[1, 0, 1],
 [1, 0, 1],    →      [0, 0, 0],
 [1, 1, 1]]           [1, 0, 1]]
 
[[0, 1, 2, 0],       [[0, 0, 0, 0],
 [3, 4, 5, 2],  →     [0, 4, 5, 0],
 [1, 3, 1, 5]]        [0, 3, 1, 0]]

The function returns nothing; the tests inspect matrix after the call.

Constraints

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