Algo-Sim › Matrix & Bit Manipulation

Set Matrix Zeroes

Medium · Matrix & Bit Manipulation · step through it in Java, Python and C++ on your own input

When a cell is 0, zero its whole row and column, in place with O(1) extra space by using row 0 and column 0 as markers.

Problem

Given an m × n integer matrix, whenever a cell holds 0, set every cell in its row and in its column to 0. Modify the matrix in place.

Constraints

Edge cases to test

Follow-up questions

Why is a separate flag needed for row 0 or column 0?
matrix[0][0] can only mark one of them; keep one boolean for the other, and clear row 0 and column 0 last.
What is the simpler version?
Two boolean arrays for rows and columns: O(m + n) extra space, the same O(mn) time.

Solution

Java

public void setZeroes(int[][] matrix) {
    int m = matrix.length, n = matrix[0].length;
    boolean firstRow = false, firstCol = false;
    for (int r = 0; r < m; r++) {
        for (int c = 0; c < n; c++) {
            if (matrix[r][c] == 0) {
                if (r == 0) firstRow = true;
                if (c == 0) firstCol = true;
                matrix[r][0] = 0;
                matrix[0][c] = 0;
            }
        }
    }
    for (int r = 1; r < m; r++) {
        for (int c = 1; c < n; c++) {
            if (matrix[r][0] == 0 || matrix[0][c] == 0) matrix[r][c] = 0;
        }
    }
    if (firstRow) for (int c = 0; c < n; c++) matrix[0][c] = 0;
    if (firstCol) for (int r = 0; r < m; r++) matrix[r][0] = 0;
}

Python

def set_zeroes(matrix):
    m, n = len(matrix), len(matrix[0])
    first_row = first_col = False
    for r in range(m):
        for c in range(n):
            if matrix[r][c] == 0:
                first_row = first_row or r == 0
                first_col = first_col or c == 0
                matrix[r][0] = 0
                matrix[0][c] = 0
    for r in range(1, m):
        for c in range(1, n):
            if matrix[r][0] == 0 or matrix[0][c] == 0:
                matrix[r][c] = 0
    if first_row:
        for c in range(n):
            matrix[0][c] = 0
    if first_col:
        for r in range(m):
            matrix[r][0] = 0

C++

#include <bits/stdc++.h>
using namespace std;

class Solution {
  public:
    void setZeroes(vector<vector<int>> &matrix) {
        int m = int(matrix.size()), n = int(matrix[0].size());
        bool firstRow = false, firstCol = false;
        for (int r = 0; r < m; r++) {
            for (int c = 0; c < n; c++) {
                if (matrix[r][c] == 0) {
                    if (r == 0)
                        firstRow = true;
                    if (c == 0)
                        firstCol = true;
                    matrix[r][0] = 0;
                    matrix[0][c] = 0;
                }
            }
        }
        for (int r = 1; r < m; r++) {
            for (int c = 1; c < n; c++) {
                if (matrix[r][0] == 0 || matrix[0][c] == 0)
                    matrix[r][c] = 0;
            }
        }
        if (firstRow)
            for (int c = 0; c < n; c++)
                matrix[0][c] = 0;
        if (firstCol)
            for (int r = 0; r < m; r++)
                matrix[r][0] = 0;
    }
};

More Matrix & Bit Manipulation problems