LeetCode_Set Matrix Zeroes

Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
(设置矩阵整行&列为0)

Example:



1. 遍历矩阵,集合维护“0”的行列号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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])

columns = set()
rows = set()

for i in range(m):
for j in range(n):
if matrix[i][j] == 0:
columns.add(j)
rows.add(i)

for i in rows:
# matrix[i] = [0]*n
for j in range(n):
matrix[i][j] = 0

for j in columns:
for i in range(m):
matrix[i][j] = 0