forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8804who.py
More file actions
23 lines (22 loc) · 702 Bytes
/
8804who.py
File metadata and controls
23 lines (22 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def numDecodings(self, s: str) -> int:
dp = [0]*(len(s)+1)
s = '0'+s
dp[0] = 1
for i in range(1, len(s)):
if int(s[i]) == 0:
if int(s[i-1]) != 1 and int(s[i-1]) != 2:
return 0
else:
dp[i]=dp[i-2]
elif int(s[i]) <= 6:
if int(s[i-1]) != 1 and int(s[i-1]) != 2:
dp[i]=dp[i-1]
else:
dp[i]=dp[i-1]+dp[i-2]
else:
if int(s[i-1]) == 1:
dp[i]=dp[i-1]+dp[i-2]
else:
dp[i]=dp[i-1]
return dp[-1]