150NeetCode 150

138.Set Matrix Zeroes

MediumBlind 75Math & Geometry · 3/8

Problem

Given an m x n integer matrix, if an element is 0, set its entire row and entire column to 0. Do it in place.

Only zeros present in the original matrix trigger this — zeros you write while processing must not spread further.

Examples

Input:  matrix = [[1,1,1],
                  [1,0,1],
                  [1,1,1]]
Output:          [[1,0,1],
                  [0,0,0],
                  [1,0,1]]
Input:  matrix = [[0,1,2,0],
                  [3,4,5,2],
                  [1,3,1,5]]
Output:          [[0,0,0,0],
                  [0,4,5,0],
                  [0,3,1,0]]

Constraints

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

💡 Key Insight

The work splits into two phases: record which rows and columns contain an original zero, then apply the zeroing. Recording needs m + n flags — and the matrix's own first row and first column can store those flags. The first row and first column then need their own separate flag (one extra boolean for column 0), and they must be zeroed last so the markers are not destroyed before they are read.

Copy the Matrix (Brute Force)

TimeO(m · n · (m + n))SpaceO(m · n)

Intuition

Zeros written during processing must not trigger more zeroing. The simplest way to guarantee that is to read from an untouched copy and write into the real matrix.

Algorithm

  1. copy = deep copy of matrix.
  2. For every (r, c) where copy[r][c] == 0: zero all of row r and column c in matrix.

Visualization

copy (read only)     matrix (written)
1 1 1                1 0 1
1 0 1       -->      0 0 0
1 1 1                1 0 1

only copy[1][1] == 0 triggers zeroing of row 1 and column 1

Code

java
class Solution {
    public void setZeroes(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        int[][] copy = new int[m][];
        for (int r = 0; r < m; r++) copy[r] = matrix[r].clone();

        for (int r = 0; r < m; r++) {
            for (int c = 0; c < n; c++) {
                if (copy[r][c] != 0) continue; // only original zeros count
                for (int k = 0; k < n; k++) matrix[r][k] = 0;
                for (int k = 0; k < m; k++) matrix[k][c] = 0;
            }
        }
    }
}

Complexity

  • Time: O(m · n · (m + n)) — each original zero clears a row and a column.
  • Space: O(m · n) — the copy.

Complexity Comparison

Approach Time Space
Copy Matrix O(m · n · (m + n)) O(m · n)
Flag Arrays O(m · n) O(m + n)
In-place Markers O(m · n) O(1)

Common Pitfalls

  • Zeroing rows/columns immediately while scanning — new zeros then trigger more zeroing and the whole matrix collapses to zeros.
  • Letting matrix[0][0] stand for both row 0 and column 0 — you need one extra boolean.
  • Processing row 0 / column 0 before the interior — that wipes out the markers you still need.
  • Using a sentinel value like -1 or Integer.MIN_VALUE for "to be zeroed" — any int can appear in the input.