R5-矩阵篇
印象题,思路即可:
手动置0
无非就是行和列都置0
使用thex和they将该元素的i和y存储起来,再分别遍历thex,将所有y的位置置0
遍历they,将所有x 置0
python
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])
thex=[]
they=[]
for i in range(m):
for j in range(n):
if matrix[i][j]==0:
thex.append(i)
they.append(j)
#清除
for i in thex:
for j in range(n):
matrix[i][j]=0
for j in they:
for i in range(m):
matrix[i][j]=0