File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def numDecodings (self , s : str ) -> int :
3+ if s [0 ] == '0' :
4+ return 0
5+
6+ n = len (s )
7+ # dp[i]๋ฅผ i๋ฒ์จฐ๊น์ง ์ฒ๋ฆฌํ์๋์ ๊ฒฝ์ฐ์ ์์ ํฉ์ผ๋ก ์ ์
8+ dp = [0 ] * n
9+ # dp[0] = 1 (์ฒซ์๋ฆฌ ์)
10+ dp [0 ] = 1
11+
12+ # i๋ฒ์จฐ ์ซ์๊ฐ 1~9๋ก ๋์ฒด๋ ์ ์์ผ๋ฉด ์ด์ ๊น์ง์ ๊ฒฝ์ฐ์ ์ ํฉ์ ์ด์ด๊ฐ
13+ # i๋ฒ์จฐ ์ซ์(๋์๋ฆฌ์)๊ฐ 10~26์ผ๋ก ๋์ฒด๋ ์ ์์ผ๋ฉด ์์๋ฆฌ๋ ์ฌ์ฉํ ๊ฒ์ด๋ฏ๋ก ๊ทธ ์์๋ฆฌ๊น์ง์ ์ํ๋ฅผ ๊ฐ์ ธ์ด
14+ # ์๊ฐ๋ณต์ก๋, ๊ณต๊ฐ๋ณต์ก๋ O(n)
15+ for i in range (1 , n ):
16+ num1 = int (s [i ])
17+ num2 = int (s [i - 1 : i + 1 ])
18+ if 1 <= num1 <= 9 :
19+ dp [i ] += dp [i - 1 ]
20+ if 10 <= num2 <= 26 :
21+ if i == 1 :
22+ dp [i ] += 1
23+ else :
24+ dp [i ] += dp [i - 2 ]
25+ return dp [n - 1 ]
You canโt perform that action at this time.
0 commit comments