Open leeway00 opened 2 years ago
O(1) space
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
N = len(matrix)
M = len(matrix[0])
first = 0 if 0 in matrix[0] else matrix[0][0]
n, m = 1,0
while n < N:
if matrix[n][m] == 0:
matrix[0][m] = 0
matrix[n][0] = 0
m+=1
if m == M:
m=0
n+=1
for n, row in enumerate(matrix[1:],1):
if row[0] == 0:
matrix[n] = [0]*M
else:
for m, val in enumerate(matrix[0]):
if val == 0:
matrix[n][m]=0
if first == 0:
matrix[0] = [0]*M