Skip to content

Commit 9e23f95

Browse files
Update matrix_zero_problem.py
1 parent eda9293 commit 9e23f95

File tree

1 file changed

+2
-12
lines changed

1 file changed

+2
-12
lines changed
+2-12
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,44 @@
1-
def setZeroes(matrix):
1+
def setzeroes(matrix):
22
rows = len(matrix)
33
cols = len(matrix[0])
4-
54
# To keep track if we need to zero out the first row and first column
65
first_row_zero = False
76
first_col_zero = False
8-
97
# Check if the first column should be zero
108
for i in range(rows):
119
if matrix[i][0] == 0:
1210
first_col_zero = True
13-
1411
# Check if the first row should be zero
1512
for j in range(cols):
1613
if matrix[0][j] == 0:
1714
first_row_zero = True
18-
1915
# Mark zero rows and columns by using the first row and first column
2016
for i in range(1, rows):
2117
for j in range(1, cols):
2218
if matrix[i][j] == 0:
2319
matrix[i][0] = 0
2420
matrix[0][j] = 0
25-
2621
# Set matrix cells to zero based on marks
2722
for i in range(1, rows):
2823
for j in range(1, cols):
2924
if matrix[i][0] == 0 or matrix[0][j] == 0:
3025
matrix[i][j] = 0
31-
3226
# Zero out the first row if needed
3327
if first_row_zero:
3428
for j in range(cols):
3529
matrix[0][j] = 0
36-
3730
# Zero out the first column if needed
3831
if first_col_zero:
3932
for i in range(rows):
4033
matrix[i][0] = 0
41-
4234
# Example input
4335
matrix = [
4436
[1, 1, 1],
4537
[1, 0, 1],
4638
[1, 1, 1]
4739
]
48-
4940
# Call the function
50-
setZeroes(matrix)
51-
41+
setzeroes(matrix)
5242
# Print the result
5343
for row in matrix:
5444
print(row)

0 commit comments

Comments
 (0)