set_matrix_zeroes

 1# @leet start
 2class Solution:
 3    def setZeroes(self, matrix: list[list[int]]) -> None:
 4        """
 5        This problem asks to set a row and col to zero if it contains
 6        an element that has a 0 in it.
 7
 8        The way we do this is by keeping track of the rows and cols that have
 9        a 0, and then for each row and col that was zero,
10        iterating through until we have set the entire row or col to 0.
11        """
12        rows = set()
13        cols = set()
14
15        m, n = len(matrix), len(matrix[0])
16
17        for y in range(m):
18            for x in range(n):
19                if matrix[y][x] == 0:
20                    rows.add(y)
21                    cols.add(x)
22
23        for row in rows:
24            for x in range(n):
25                matrix[row][x] = 0
26
27        for col in cols:
28            for y in range(m):
29                matrix[y][col] = 0
30
31
32# @leet end
33
34
35def test():
36    assert 2 + 2 == 4
class Solution:
 3class Solution:
 4    def setZeroes(self, matrix: list[list[int]]) -> None:
 5        """
 6        This problem asks to set a row and col to zero if it contains
 7        an element that has a 0 in it.
 8
 9        The way we do this is by keeping track of the rows and cols that have
10        a 0, and then for each row and col that was zero,
11        iterating through until we have set the entire row or col to 0.
12        """
13        rows = set()
14        cols = set()
15
16        m, n = len(matrix), len(matrix[0])
17
18        for y in range(m):
19            for x in range(n):
20                if matrix[y][x] == 0:
21                    rows.add(y)
22                    cols.add(x)
23
24        for row in rows:
25            for x in range(n):
26                matrix[row][x] = 0
27
28        for col in cols:
29            for y in range(m):
30                matrix[y][col] = 0
def setZeroes(self, matrix: list[list[int]]) -> None:
 4    def setZeroes(self, matrix: list[list[int]]) -> None:
 5        """
 6        This problem asks to set a row and col to zero if it contains
 7        an element that has a 0 in it.
 8
 9        The way we do this is by keeping track of the rows and cols that have
10        a 0, and then for each row and col that was zero,
11        iterating through until we have set the entire row or col to 0.
12        """
13        rows = set()
14        cols = set()
15
16        m, n = len(matrix), len(matrix[0])
17
18        for y in range(m):
19            for x in range(n):
20                if matrix[y][x] == 0:
21                    rows.add(y)
22                    cols.add(x)
23
24        for row in rows:
25            for x in range(n):
26                matrix[row][x] = 0
27
28        for col in cols:
29            for y in range(m):
30                matrix[y][col] = 0

This problem asks to set a row and col to zero if it contains an element that has a 0 in it.

The way we do this is by keeping track of the rows and cols that have a 0, and then for each row and col that was zero, iterating through until we have set the entire row or col to 0.

def test():
36def test():
37    assert 2 + 2 == 4