基本思路,先对数组全遍历一次,记录出现0的坐标,然后对整行和整列分别遍历对应坐标即可。代码如下:
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
m,n=len(matrix),len(matrix[0])
tmp1=[]
tmp2=[]
for i in range (m):
for j in range (n):
if matrix[i][j]==0:
tmp1.append(i)
tmp2.append(j)
for t in tmp1:
for i in range (n):
matrix[t][i]=0
for t in tmp2:
for i in range (m):
matrix[i][t]=0
return matrix