noodleProblems/
Set Matrix Zeroes
#80

Set Matrix Zeroes

AlgorithmmediumArrayHash TableMatrix

Given an m x n integer matrix, if any cell holds 0, set its **entire row and column** to 0.

Do this **in place**: mutate the input matrix directly and return that same matrix (do not allocate a new one).

Example cases

  • single zero
    in matrix =
    111
    101
    111
    out [[1,0,1],[0,0,0],[1,0,1]]
    The zero at (1,1) clears row 1 and column 1.
  • two zeroes
    in matrix =
    0120
    3452
    1315
    out [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
  • no zero
    in matrix =
    12
    34
    out [[1,2],[3,4]]

Constraints

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