Skip to main content
Medium Matrix High frequency

Set Matrix Zeroes

Open on LeetCode

Approach Summary

Use first row and column as markers. Mark zero locations, then zero out rows/columns accordingly.

Full Solution & Approach

The challenge is the O(1) space constraint. Instead of a separate row/column marker array, use the first row and first column as markers. First save their original state by checking whether row 0 and column 0 contain any zero. Then scan the matrix; for every cell that is zero, mark its row and column by writing 0 to matrix[i][0] and matrix[0][j]. Zero out the inner cells next: any cell whose row marker or column marker is 0 becomes 0. Finally, handle the first row and first column using the saved flags. The two-phase approach — mark, then apply — ensures markers are read before being overwritten, which is the classic trap in this problem.

Two passes over the matrix — O(m × n) time. Only two boolean flags plus the matrix itself — O(1) extra space.

Solution Code

Solution

def set_zeroes(matrix: list[list[int]]) -> None:
    m, n = len(matrix), len(matrix[0])
    first_row = any(matrix[0][j] == 0 for j in range(n))
    first_col = any(matrix[i][0] == 0 for i in range(m))
    for i in range(1, m):
        for j in range(1, n):
            if matrix[i][j] == 0:
                matrix[i][0] = 0
                matrix[0][j] = 0
    for i in range(1, m):
        for j in range(1, n):
            if matrix[i][0] == 0 or matrix[0][j] == 0:
                matrix[i][j] = 0
    if first_row:
        for j in range(n):
            matrix[0][j] = 0
    if first_col:
        for i in range(m):
            matrix[i][0] = 0

Edge Cases to Watch

  • A zero in the first row or column — the saved flags preserve them
  • Single cell [0] — first_row and first_col are both true
  • All zeros — the whole matrix is already zero
  • No zeros — returned unchanged

How to Recognize This Pattern

  • Zero out rows and columns in-place
  • Use first row/col as flags

Complexity Analysis

Time Complexity

O(m × n)

Space Complexity

O(1)

Tags

Array Matrix

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee