矩阵置0
题目:矩阵置0
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
m = len(matrix)
n = len(matrix[0])
rows = [0] * m
columns = [0] * n
for i in range(m):
for j in range(n):
if(matrix[i][j]==0):
rows[i] = 1
columns[j] = 1
print(rows,columns)
for i in range(m):
for j in range(n):
if(rows[i] or columns[j]):
matrix[i][j] = 0
```
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
m, n = len(matrix), len(matrix[0])
flag_col0 = False
for i in range(m):
if matrix[i][0] == 0:
flag_col0 = True
for j in range(1, n):
if matrix[i][j] == 0:
matrix[i][0] = matrix[0][j] = 0
for i in range(m - 1, -1, -1):
for j in range(1, n):
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0
if flag_col0:
matrix[i][0] = 0