We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ffaa0f7 commit f3d756fCopy full SHA for f3d756f
โdecode-ways/yyyyyyyyyKim.pyโ
@@ -0,0 +1,24 @@
1
+class Solution:
2
+ def numDecodings(self, s: str) -> int:
3
+
4
+ # DP
5
+ dp = [0]*(len(s)+1)
6
7
+ # s๊ฐ 0์ผ๋ก ์์ํ๋ฉด 0 return
8
+ if s[0] == '0':
9
+ return 0
10
11
+ dp[0] = 1 # ๋น๋ฌธ์์ด์ ํด์๊ฐ๋ฅํ 1๊ฐ์ง ๊ฒฝ์ฐ๋ก ์ทจ๊ธ (์ด๊ธฐ๊ธฐ์ค์ ์ญํ , dp[i-2]๊ณ์ฐ์ํ์)
12
+ dp[1] = 1 # ์ฒซ๋ฒ์งธ์๋ฆฌ์ ์ฒ๋ฆฌ๋ฐฉ๋ฒ์ 1๊ฐ์ง
13
14
+ # len(s)๊ฐ 2 ์ด์์ผ๋
15
+ for i in range(2,len(s)+1):
16
+ one = int(s[i-1]) # ํ์๋ฆฌ(ํ์ฌ์๋ฆฌ)
17
+ two = int(s[i-2:i]) # ํ์๋ฆฌ + ์์๋ฆฌ = ๋์๋ฆฌ
18
19
+ if 1 <= one <= 9:
20
+ dp[i] += dp[i-1]
21
+ if 10 <= two <= 26:
22
+ dp[i] += dp[i-2]
23
24
+ return dp[len(s)]
0 commit comments