-
Notifications
You must be signed in to change notification settings - Fork 1
/
_073_SetMatrixZeroes.py
66 lines (55 loc) · 1.94 KB
/
_073_SetMatrixZeroes.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#-----------------------------------------------------------------------------
# Runtime: 148ms
# Memory Usage:
# Link:
#-----------------------------------------------------------------------------
class Solution:
def setZeroes(self, matrix: [[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
row_length = len(matrix)
if row_length == 0: return
col_length = len(matrix[0])
update_place = []
for i in range(row_length):
for j in range(col_length):
if matrix[i][j] == 0:
update_place.append((i,j))
for row, col in update_place:
for i in range(row_length):
matrix[i][col] = 0
for i in range(col_length):
matrix[row][i] = 0
def setZeroes_2(self, matrix: [[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
row_length = len(matrix)
if row_length == 0: return
col_length = len(matrix[0])
first_column_zero = False
first_row_zero = False
for i in range(row_length):
if matrix[i][0] == 0:
first_column_zero = True
break
for i in range(col_length):
if matrix[0][i] == 0:
first_row_zero = True
break
for i in range(row_length):
for j in range(col_length):
if matrix[i][j] == 0:
matrix[i][0] = 0
matrix[0][j] = 0
for i in range(1, row_length):
for j in range(1, col_length):
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0
if first_column_zero:
for i in range(row_length):
matrix[i][0] = 0
if first_row_zero:
for i in range(col_length):
matrix[0][i] = 0