File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution (object ):
2+ def setZeroes (self , matrix ):
3+ """
4+ :type matrix: List[List[int]]
5+ :rtype: None Do not return anything, modify matrix in-place instead.
6+ """
7+ rows = len (matrix )
8+ cols = len (matrix [0 ])
9+ col0 = 1
10+
11+ # Step 1: Use first row and column as markers
12+ for i in range (rows ):
13+ if matrix [i ][0 ] == 0 :
14+ col0 = 0
15+ for j in range (1 , cols ):
16+ if matrix [i ][j ] == 0 :
17+ matrix [i ][0 ] = 0
18+ matrix [0 ][j ] = 0
19+
20+ # Step 2: Update cells based on markers
21+ for i in range (1 , rows ):
22+ for j in range (1 , cols ):
23+ if matrix [i ][0 ] == 0 or matrix [0 ][j ] == 0 :
24+ matrix [i ][j ] = 0
25+
26+ # Step 3: Handle first row
27+ if matrix [0 ][0 ] == 0 :
28+ for j in range (cols ):
29+ matrix [0 ][j ] = 0
30+
31+ # Step 4: Handle first column
32+ if col0 == 0 :
33+ for i in range (rows ):
34+ matrix [i ][0 ] = 0
35+
You can’t perform that action at this time.
0 commit comments