Aditya Saini’s blog

Pure Computer Science Knowledge Portal

Set Matrix Zero, LeetCode 73 Solution in Python 3

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Example 1: Input: [ [1,1,0], [1,0,1], [1,1,1] ] Output: [ [0,0,0], [0,0,0], [1,0,0] ] Here the approach is very simple. We just have to traverse the matrix and note down the rows and columns indexes which are required to be zero, for that we will use set data structure as it has a constant O(1) search complexity. Then we will again traverse the matrix and will make the element zero if its row or its column is in our set.Below is code of this approach in Python. Just try to do it yourself and then only look into the code otherwise it will hinder your growth.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
 class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        rows = len(matrix)
        if  rows > 0:
            cols = len(matrix[0])
        zero_rows = set()
        zero_cols = set()
        
        for r in range(rows):
            for c in range(cols):
                if matrix[r][c] == 0:
                    zero_rows.add(r)
                    zero_cols.add(c)
        for r in range(rows):
            for c in range(cols):
                if r in zero_rows or c in zero_cols:
                    matrix[r][c] = 0