← DSA
MEDIUM

Set Matrix Zeroes

PracticeNeetCode ↗LeetCode ↗

Given an m × n matrix, if a cell is 0, set its entire row and column to 0 — in place.

Example

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

Key Pointers

Step-by-Step Analysis

  1. Scan the whole matrix once, recording every row index and column index that contains a 0, into two sets.
  2. Scan the matrix a second time; for any cell whose row or column is in either set, set that cell to 0.
  3. Return — the original zero positions are still correctly zero, and nothing cascaded.

Complexity

Time: O(m · n) — Space: O(m + n)

Code

def setZeroes(matrix: list[list[int]]) -> None:
    rows, cols = set(), set()

    for r in range(len(matrix)):
        for c in range(len(matrix[0])):
            if matrix[r][c] == 0:
                rows.add(r)
                cols.add(c)

    for r in range(len(matrix)):
        for c in range(len(matrix[0])):
            if r in rows or c in cols:
                matrix[r][c] = 0

Code Explanation

How the solution above works, piece by piece.

  1. First pass: collect the indices of every row and column that contains a 0.
  2. Second pass: set a cell to 0 if its row or its column was recorded.
  3. Separating the passes prevents freshly written zeros from being mistaken for original ones.
  4. O(m × n) time, O(m + n) extra space (a constant-space version stores the flags in the first row and column).

Visual Walkthrough

Running it on matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]:

Zero in the centre spreads to its row and columninput111101111rows {1}cols {1}output101000101
Pass 1 only records which rows and columns contain a zero. Pass 2 zeroes those rows and columns. Recording first matters: zeroing while scanning would create new zeros that wrongly spread further.
original zeroset to zero

Video Explanation

NeetCode — Set Matrix Zeroes, LeetCode 73