Skip to content

Commit dc67c5e

Browse files
authored
Create 73-Set-Matrix-Zeroes.py
1 parent 1206a62 commit dc67c5e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

73-Set-Matrix-Zeroes.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def setZeroes(self, matrix: List[List[int]]) -> None:
3+
# O(1)
4+
ROWS, COLS = len(matrix), len(matrix[0])
5+
rowZero = False
6+
7+
# determine which rows/cols need to be zero
8+
for r in range(ROWS):
9+
for c in range(COLS):
10+
if matrix[r][c] == 0:
11+
matrix[0][c] = 0
12+
if r > 0:
13+
matrix[r][0] = 0
14+
else:
15+
rowZero = True
16+
17+
for r in range(1, ROWS):
18+
for c in range(1, COLS):
19+
if matrix[0][c] == 0 or matrix[r][0] == 0:
20+
matrix[r][c] = 0
21+
22+
if matrix[0][0] == 0:
23+
for r in range(ROWS):
24+
matrix[r][0] = 0
25+
26+
if rowZero:
27+
for c in range(COLS):
28+
matrix[0][c] = 0

0 commit comments

Comments
 (0)