Given a two-dimensional matrix of integers, for each zero in the original matrix, replace all values in its row and column with zero, and return the resulting matrix.
We want to first iterate through the matrix and count the rows and cols that have a 0, to zero fill later. We'll use two sets for this, rows and cols. Then, we'll iterate again, and if our row number is in rows or our col number is in cols, we'll set the current cell to 0. Easy.
class Solution:
def solve(self, matrix):
= len(matrix)
row_len = len(matrix[0])
col_len = set()
rows = set()
cols
for i in range(row_len):
for j in range(col_len):
if matrix[i][j] == 0:
rows.add(i)
cols.add(j)
for i in range(row_len):
for j in range(col_len):
if i in rows or j in cols:
= 0
matrix[i][j]
return matrix