Skip to content

Commit ba93191

Browse files
author
sangbeenmoon
committed
solved decode-ways.
1 parent 9b7fd85 commit ba93191

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

โ€Žcombination-sum/sangbeenmoon.pyโ€Ž

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,3 @@ def go(self, i:int, candidates: List[int], nums: List[int], sum_: int, target: i
1818
nums.append(candidates[j])
1919
self.go(j, candidates, nums, sum_ + candidates[j], target)
2020
nums.pop()
21-
22-
23-
24-
25-
26-
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# idea
2+
# ๋งˆ์ง€๋ง‰ ๋‘์ž๋ฆฌ ์ˆ˜๊ฐ€ ๋…๋ฆฝ ๊ฐ€๋Šฅ -> dp[i] = dp[i-1] + dp[i-2]
3+
# ๋งˆ์ง€๋ง‰ ํ•œ์ž๋ฆฌ ์ˆ˜๊ฐ€ ๋…๋ฆฝ ๊ฐ€๋Šฅ -> dp[i] = dp[i-1]
4+
# ์œ„ ๋‘ ์ผ€์ด์Šค ๋ชจ๋‘ ๋ถˆ๊ฐ€๋Šฅ -> 0 ๋ฐ˜ํ™˜
5+
# TC : O(n)
6+
# SC : O(n)
7+
8+
class Solution:
9+
def numDecodings(self, s: str) -> int:
10+
dp = [1] * len(s)
11+
12+
if s[0] == '0':
13+
return 0
14+
15+
if '1' <= s[0] and s[0] <= '9':
16+
dp[0] = 1
17+
18+
for i in range(1, len(s)):
19+
target = s[i-1:i+1]
20+
21+
if int(s[i]) != 0 and 10 <= int(target) and int(target) <= 26:
22+
dp[i] = dp[i-1] + dp[i-2]
23+
continue
24+
25+
if int(s[i]) == 0 and 10 <= int(target) and int(target) <= 26:
26+
dp[i] = dp[i-2]
27+
continue
28+
29+
target = s[i]
30+
if 1 <= int(target) and int(target) <= 9:
31+
dp[i] = dp[i-1]
32+
continue
33+
34+
return 0
35+
36+
return dp[len(s) - 1]

0 commit comments

Comments
ย (0)