File tree 1 file changed +2
-12
lines changed
1 file changed +2
-12
lines changed Original file line number Diff line number Diff line change 1
- def setZeroes (matrix ):
1
+ def setzeroes (matrix ):
2
2
rows = len (matrix )
3
3
cols = len (matrix [0 ])
4
-
5
4
# To keep track if we need to zero out the first row and first column
6
5
first_row_zero = False
7
6
first_col_zero = False
8
-
9
7
# Check if the first column should be zero
10
8
for i in range (rows ):
11
9
if matrix [i ][0 ] == 0 :
12
10
first_col_zero = True
13
-
14
11
# Check if the first row should be zero
15
12
for j in range (cols ):
16
13
if matrix [0 ][j ] == 0 :
17
14
first_row_zero = True
18
-
19
15
# Mark zero rows and columns by using the first row and first column
20
16
for i in range (1 , rows ):
21
17
for j in range (1 , cols ):
22
18
if matrix [i ][j ] == 0 :
23
19
matrix [i ][0 ] = 0
24
20
matrix [0 ][j ] = 0
25
-
26
21
# Set matrix cells to zero based on marks
27
22
for i in range (1 , rows ):
28
23
for j in range (1 , cols ):
29
24
if matrix [i ][0 ] == 0 or matrix [0 ][j ] == 0 :
30
25
matrix [i ][j ] = 0
31
-
32
26
# Zero out the first row if needed
33
27
if first_row_zero :
34
28
for j in range (cols ):
35
29
matrix [0 ][j ] = 0
36
-
37
30
# Zero out the first column if needed
38
31
if first_col_zero :
39
32
for i in range (rows ):
40
33
matrix [i ][0 ] = 0
41
-
42
34
# Example input
43
35
matrix = [
44
36
[1 , 1 , 1 ],
45
37
[1 , 0 , 1 ],
46
38
[1 , 1 , 1 ]
47
39
]
48
-
49
40
# Call the function
50
- setZeroes (matrix )
51
-
41
+ setzeroes (matrix )
52
42
# Print the result
53
43
for row in matrix :
54
44
print (row )
You can’t perform that action at this time.
0 commit comments