Skip to content
Decoder

Set Matrix Zeroes

Medium

Problem

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.

Example 1

[[1, 1, 1],          [[1, 0, 1],

Example 2

Input
[1, 0, 1],
Output
[0, 0, 0],

Example 3

 [1, 1, 1]]           [1, 0, 1]]

Example 4

[[0, 1, 2, 0],       [[0, 0, 0, 0],

Example 5

Input
[3, 4, 5, 2],
Output
[0, 4, 5, 0],

Example 6

 [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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.