File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ 시간복잡도: O(m * n)
3
+ 공간복잡도: O(1)
4
+ '''
5
+
6
+ from typing import List
7
+
8
+
9
+ class Solution :
10
+ def setZeroes (self , matrix : List [List [int ]]) -> None :
11
+ m , n = len (matrix ), len (matrix [0 ])
12
+ first_row_zero = False # Flag to check if the first row needs to be zeroed
13
+ first_col_zero = False # Flag to check if the first column needs to be zeroed
14
+
15
+ # Check if the first row has any zeros
16
+ for j in range (n ):
17
+ if matrix [0 ][j ] == 0 :
18
+ first_row_zero = True
19
+ break
20
+
21
+ # Check if the first column has any zeros
22
+ for i in range (m ):
23
+ if matrix [i ][0 ] == 0 :
24
+ first_col_zero = True
25
+ break
26
+
27
+ # Use the first row and column to mark rows and columns that need to be zeroed
28
+ for i in range (1 , m ):
29
+ for j in range (1 , n ):
30
+ if matrix [i ][j ] == 0 :
31
+ matrix [i ][0 ] = 0
32
+ matrix [0 ][j ] = 0
33
+
34
+ # Zero out cells based on markers in the first row and column
35
+ for i in range (1 , m ):
36
+ for j in range (1 , n ):
37
+ if matrix [i ][0 ] == 0 or matrix [0 ][j ] == 0 :
38
+ matrix [i ][j ] = 0
39
+
40
+ # Zero out the first row if needed
41
+ if first_row_zero :
42
+ for j in range (n ):
43
+ matrix [0 ][j ] = 0
44
+
45
+ # Zero out the first column if needed
46
+ if first_col_zero :
47
+ for i in range (m ):
48
+ matrix [i ][0 ] = 0
You can’t perform that action at this time.
0 commit comments