73. 矩阵置零
class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
m = len(matrix)
n = len(matrix[0])
r, c = set(), set()
for i in range(m):
for j in range(n):
if matrix[i][j] == 0:
r.add(i)
c.add(j)
for i in range(m):
for j in range(n):
if i in r or j in c:
matrix[i][j] = 0
思路:
行列分别用set储存一遍。
综上,时间复杂度O(m*n),空间复杂度是O(m+n)。