-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path91.解码方法.py
More file actions
65 lines (60 loc) · 1.29 KB
/
91.解码方法.py
File metadata and controls
65 lines (60 loc) · 1.29 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#
# @lc app=leetcode.cn id=91 lang=python
#
# [91] 解码方法
#
# https://leetcode-cn.com/problems/decode-ways/description/
#
# algorithms
# Medium (21.68%)
# Likes: 196
# Dislikes: 0
# Total Accepted: 16.8K
# Total Submissions: 76.8K
# Testcase Example: '"12"'
#
# 一条包含字母 A-Z 的消息通过以下方式进行了编码:
#
# 'A' -> 1
# 'B' -> 2
# ...
# 'Z' -> 26
#
#
# 给定一个只包含数字的非空字符串,请计算解码方法的总数。
#
# 示例 1:
#
# 输入: "12"
# 输出: 2
# 解释: 它可以解码为 "AB"(1 2)或者 "L"(12)。
#
#
# 示例 2:
#
# 输入: "226"
# 输出: 3
# 解释: 它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。
#
#
#
class Solution(object):
def numDecodings(self, s):
memo=[None]*len(s)
def rec(memo,ind,s):
if len(s)==1 and s[0]=='0':
memo[ind]=0
return 0
if len(s)==1 or not s:return 1
if s[0]=='0':
memo[ind]=0
return 0
if memo[ind]:return memo[ind]
m=0
n=0
m=rec(memo,ind+1,s[1:])
if int(s[:2])<27:
n=rec(memo,ind+2,s[2:])
memo[ind]=m+n
return memo[ind]
return rec(memo,0,s)