</>

73. Set Matrix Zeroes

Medium NeetCode 150
Math & Geometry Time O(mn) Space O(m+n)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Math & Geometry
Time
O(mn)
Space
O(m+n)

Standard Solution (C++)

SummaryGiven an m x n integer matrix, find every cell containing zero and set its entire row and column to zero, modifying the matrix in-place.

Approach
  • Use the first row and first column as sentinel arrays to record which rows/columns contain a zero, avoiding extra O(m+n) space.
  • A separate boolean tracks whether the first row itself originally had a zero (since it doubles as a sentinel).
  • In a second pass, use the sentinels to zero out the appropriate rows and columns, then handle the first row/column last to avoid corrupting the sentinel data early.
  • This achieves O(1) auxiliary space while keeping O(m·n) time.
#include <vector>
using namespace std;

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m = matrix.size(), n = matrix[0].size();

        // Does the first row itself contain a zero?
        bool firstRowZero = false;
        for (int j = 0; j < n; ++j)
            if (matrix[0][j] == 0) { firstRowZero = true; break; }

        // Use row 0 and col 0 as sentinels for the rest of the matrix.
        // We skip row 0 (handled separately) and use matrix[i][0] for rows.
        for (int i = 1; i < m; ++i)
            for (int j = 0; j < n; ++j)
                if (matrix[i][j] == 0) {
                    matrix[i][0] = 0;   // mark row i
                    matrix[0][j] = 0;   // mark col j
                }

        // Zero out cells guided by sentinels (skip first row/col for now).
        for (int i = 1; i < m; ++i)
            for (int j = 1; j < n; ++j)
                if (matrix[i][0] == 0 || matrix[0][j] == 0)
                    matrix[i][j] = 0;

        // Zero out first column if any of rows 1..m-1 was a zero-row.
        if (matrix[0][0] == 0)
            for (int i = 1; i < m; ++i)
                matrix[i][0] = 0;

        // Zero out first row if it originally contained a zero.
        if (firstRowZero)
            for (int j = 0; j < n; ++j)
                matrix[0][j] = 0;
    }
};
Time O(m·n) Space O(1)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m=matrix.size();
        int n=matrix[0].size();

        vector<bool> row(m, false);
        vector<bool> col(n, false);

        for(int i=0; i<m; ++i){ // O(mn)
            for(int j=0; j<n; ++j){
                if(matrix[i][j]==0){
                    row[i]=true;
                    col[j]=true;
                }
            }
        }

        for(int i=0; i<m; ++i){ // O(mn)
            if(row[i]){
                for(int j=0; j<n; ++j){
                    matrix[i][j]=0;
                }
            }
        }

        for(int j=0; j<n; ++j){ // O(mn)
            if(col[j]){
                for(int i=0; i<m; ++i){
                    matrix[i][j]=0;
                }
            }
        }

    }
};

// Time Complexity: O(mn)
// Space Complexity: O(m+n), can be optimized to O(1) by using the first row and first column to store the information.
Time O(mn) Space O(m+n)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern