File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+
3+ ํ์ด :
4+ dfs๋ฅผ ์ด์ฉํด์ ์ธ์๋ก ๋ค์ด์จ row, col๋ถํฐ matrix๋ฅผ ์ํํ๋ฉฐ 0์ ์ฐพ๋๋ค
5+ 0 ์ฐพ์ผ๋ฉด column 1 ์ฆ๊ฐ์์ผ dfsํธ์ถ ํ ํด๋น ํ,์ด์ 0์ผ๋ก ์ค์ ํ return
6+ ๋ชจ๋ matrix ์ํํ๋ฉด return
7+
8+ m * n matrix
9+
10+ TC : O(M * N)
11+ ์ ์ฒด matrix๋ฅผ ํ๋ฒ ์ํํ๋ฏ๋ก
12+
13+ SC : O(M * N)
14+ 0์ ๊ฐ์๋งํผ ์ฌ๊ทํธ์ถ์คํ์ด ์์ด๋๋ฐ ์ต์
์ ๊ฒฝ์ฐ M * N๋งํผ ํธ์ถ๋๋ฏ๋ก
15+
16+
17+ - ๋ค๋ฅธํ์ด
18+ ์ฒซ์งธ ํ๊ณผ ์ฒซ์งธ ์ด์ ๊ฐ ํ๋ ฌ์ ๋ํ 0์ฌ๋ถ ์ ์ฅํ๋ flag๋ก ์ฌ์ฉ
19+ ์ฒซ์งธ ํ๊ณผ ์ฒซ์งธ ์ด์ 0 ์ฌ๋ถ๋ ๋ฐ๋ก ๋ณ์ 2๊ฐ๋ก ์ ์ฅ
20+ ๊ณต๊ฐ๋ณต์ก๋๋ฅผ O(1)๋ก ๊ฐ์ ํ ์ ์์
21+ """
22+
23+ class Solution :
24+ def setZeroes (self , matrix : List [List [int ]]) -> None :
25+ """
26+ Do not return anything, modify matrix in-place instead.
27+ """
28+ n_rows = len (matrix )
29+ n_cols = len (matrix [0 ])
30+ def dfs (row : int , col : int ) -> None :
31+ while row < n_rows :
32+ while col < n_cols :
33+ if matrix [row ][col ] == 0 :
34+ dfs (row ,col + 1 )
35+ for i in range (n_rows ) :
36+ matrix [i ][col ] = 0
37+ for j in range (n_cols ) :
38+ matrix [row ][j ] = 0
39+ return
40+ col += 1
41+ col = 0
42+ row += 1
43+ return
44+ dfs (0 ,0 )
You canโt perform that action at this time.
0 commit comments