Skip to content

Commit f050ff6

Browse files
authored
Created set_matrix_zeroes.py
1 parent 788d95b commit f050ff6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution(object):
2+
def setZeroes(self, matrix):
3+
"""
4+
:type matrix: List[List[int]]
5+
:rtype: None Do not return anything, modify matrix in-place instead.
6+
"""
7+
rows = len(matrix)
8+
cols = len(matrix[0])
9+
col0 = 1
10+
11+
# Step 1: Use first row and column as markers
12+
for i in range(rows):
13+
if matrix[i][0] == 0:
14+
col0 = 0
15+
for j in range(1, cols):
16+
if matrix[i][j] == 0:
17+
matrix[i][0] = 0
18+
matrix[0][j] = 0
19+
20+
# Step 2: Update cells based on markers
21+
for i in range(1, rows):
22+
for j in range(1, cols):
23+
if matrix[i][0] == 0 or matrix[0][j] == 0:
24+
matrix[i][j] = 0
25+
26+
# Step 3: Handle first row
27+
if matrix[0][0] == 0:
28+
for j in range(cols):
29+
matrix[0][j] = 0
30+
31+
# Step 4: Handle first column
32+
if col0 == 0:
33+
for i in range(rows):
34+
matrix[i][0] = 0
35+

0 commit comments

Comments
 (0)