forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0091.py
More file actions
28 lines (23 loc) · 694 Bytes
/
0091.py
File metadata and controls
28 lines (23 loc) · 694 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution:
def numDecodings(self, s):
"""
:type s: str
:rtype: int
"""
if not s or s.startswith('0'):
return 0
if len(s) == 1 and s[0] != '0':
return 1
s_len = len(s)
mem = [0 for _ in range(s_len + 1)]
mem[0] = 1 if s[0] != '0' else 0
mem[1] = 1 if s[1] != '0' else 0
for i in range(2, s_len + 1):
if 1 <= int(s[i - 1]) <= 9:
mem[i] += mem[i - 1]
if 10 <= int(s[i-2:i]) <= 26:
mem[i] += mem[i - 2]
return mem[-1]
if __name__ == '__main__':
s = "12120"
print(Solution().numDecodings(s))