File tree Expand file tree Collapse file tree 1 file changed +10
-9
lines changed
Python/chapter01/1.8 - Zero Matrix Expand file tree Collapse file tree 1 file changed +10
-9
lines changed Original file line number Diff line number Diff line change @@ -22,19 +22,20 @@ def zero_matrix(matrix: List[List[int]]) -> List[List[int]]:
22
22
zeroed_rows = set ()
23
23
zeroed_cols = set ()
24
24
25
- for row in range ( M ):
26
- for col , num in enumerate (matrix [ row ] ):
27
- if num != 0 or row in zeroed_rows or col in zeroed_cols :
25
+ for i , row in enumerate ( matrix ):
26
+ for j , num in enumerate (row ):
27
+ if num != 0 or i in zeroed_rows or j in zeroed_cols :
28
28
continue
29
29
# otherwise, set row to 0 by looping through columns of current row
30
- for i in range (N ):
31
- matrix [row ][ i ] = 0
30
+ for k in range (N ):
31
+ matrix [i ][ k ] = 0
32
32
# set column to 0 by looping through rows of current column
33
- for j in range (M ):
34
- matrix [j ][ col ] = 0
33
+ for l in range (M ):
34
+ matrix [l ][ j ] = 0
35
35
# update zeroed row and col sets
36
- zeroed_cols .add (col )
37
- zeroed_rows .add (row )
36
+ zeroed_cols .add (j )
37
+ zeroed_rows .add (i )
38
+ break
38
39
return matrix
39
40
40
41
You can’t perform that action at this time.
0 commit comments