LC0073 - Set Matrix Zeroes (MEDIUM)#

This is the solution for Leetcode Problem 73 - Set Matrix Zeroes.

Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0’s, and return the matrix.

You must do it in place.

Examples:

Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
 1"""Solution to Leetcode 73 - 73
 2
 3Link : https://leetcode.com/problems/set-matrix-zeroes/
 4Level: medium
 5"""
 6
 7from typing import List
 8
 9
10class Solution:
11    def setZeroes(self, matrix: List[List[int]]) -> None:
12        """
13        Do not return anything, modify matrix in-place instead.
14        """
15        # first note down where the zeroes are.
16        zeroes = []
17        for i, row in enumerate(matrix):
18            for j, cell in enumerate(row):
19                if cell == 0:
20                    zeroes.append((i, j))
21        for (i, j) in zeroes:
22            # item at i x j is 0,
23            # set row i to zero
24            # set col j to zero
25            matrix[i] = [0 for _ in matrix[i]]
26            for row, _ in enumerate(matrix):
27                matrix[row][j] = 0