-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
38 lines (30 loc) · 1 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from typing import List
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
rows, cols = len(matrix), len(matrix[0])
rowZero = False
for r in range(rows):
for c in range(cols):
if matrix[r][c] == 0:
matrix[0][c] = 0
if r == 0:
rowZero = True
else:
matrix[r][0] = 0
for r in range(1, rows):
for c in range(1, cols):
if matrix[0][c] == 0 or matrix[r][0] == 0:
matrix[r][c] = 0
if matrix[0][0] == 0:
for r in range(rows):
matrix[r][0] = 0
if rowZero:
for c in range(cols):
matrix[0][c] = 0
return matrix
if __name__ == '__main__':
matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
print(Solution().setZeroes(matrix))