File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Solution :
5+ def setZeroes (self , matrix : List [List [int ]]) -> None :
6+ """
7+ Do not return anything, modify matrix in-place instead.
8+ """
9+ if not matrix or not matrix [0 ]:
10+ return
11+
12+ rows , cols = len (matrix ), len (matrix [0 ])
13+ first_row_has_zero = any (matrix [0 ][j ] == 0 for j in range (cols ))
14+ first_col_has_zero = any (matrix [i ][0 ] == 0 for i in range (rows ))
15+
16+ # Use the first row and first column to mark zeros
17+ for i in range (1 , rows ):
18+ for j in range (1 , cols ):
19+ if matrix [i ][j ] == 0 :
20+ matrix [0 ][j ] = 0
21+ matrix [i ][0 ] = 0
22+
23+ # Set matrix elements to zero based on marks
24+ for i in range (1 , rows ):
25+ for j in range (1 , cols ):
26+ if matrix [0 ][j ] == 0 or matrix [i ][0 ] == 0 :
27+ matrix [i ][j ] = 0
28+
29+ # Set the first row to zero if needed
30+ if first_row_has_zero :
31+ for j in range (cols ):
32+ matrix [0 ][j ] = 0
33+
34+ # Set the first column to zero if needed
35+ if first_col_has_zero :
36+ for i in range (rows ):
37+ matrix [i ][0 ] = 0
You can’t perform that action at this time.
0 commit comments