-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathmatrix_zero_problem.py
44 lines (44 loc) · 1.28 KB
/
matrix_zero_problem.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
def setzeroes(matrix):
rows = len(matrix)
cols = len(matrix[0])
# To keep track if we need to zero out the first row and first column
first_row_zero = False
first_col_zero = False
# Check if the first column should be zero
for i in range(rows):
if matrix[i][0] == 0:
first_col_zero = True
# Check if the first row should be zero
for j in range(cols):
if matrix[0][j] == 0:
first_row_zero = True
# Mark zero rows and columns by using the first row and first column
for i in range(1, rows):
for j in range(1, cols):
if matrix[i][j] == 0:
matrix[i][0] = 0
matrix[0][j] = 0
# Set matrix cells to zero based on marks
for i in range(1, rows):
for j in range(1, cols):
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0
# Zero out the first row if needed
if first_row_zero:
for j in range(cols):
matrix[0][j] = 0
# Zero out the first column if needed
if first_col_zero:
for i in range(rows):
matrix[i][0] = 0
# Example input
matrix = [
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
]
# Call the function
setzeroes(matrix)
# Print the result
for row in matrix:
print(row)