[LeetCode]题解(python):091 Decode Ways
题目来源
https://leetcode.com/problems/decode-ways/
A message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
The number of ways decoding "12" is 2.
题意分析
Input: 一个字符串
Output:可以编码的方式数目
Conditions:a到z分别对应1到26
题目思路
采用动态规划的思想,dp初始化为[1,1],以两个字符来考虑
1)如果10 <= int(s[i-2:i]) <= 26 and s[i - 1] != "0",说明有两种选择方式,dp[i] = dp[i-1] + dp[i-2]
2)如果刚好等于10或者20,那么dp[i] = dp[i-2]
3)如果s[i-1]不为0,说明不是30,40等数字,dp[i] = dp[i-1]
4)如果不满足以上条件,那么不可能编码成功,返回0
AC代码(Python)
class Solution(object):
def numDecodings(self, s):
"""
:type s: str
:rtype: int
"""
if s == "" or s[0] == "":
return 0
dp = [1,1]
for i in range(2, len(s) + 1):
if 10 <= int(s[i-2:i]) <= 26 and s[i - 1] != "":
dp.append(dp[i-1] + dp[i-2])
elif int(s[i-2:i]) == 10 or int(s[i-2:i]) == 20:
dp.append(dp[i-2])
elif s[i-1] != "":
dp.append(dp[i-1])
else:
return 0
return dp[len(s)]
[LeetCode]题解(python):091 Decode Ways的更多相关文章
- 【LeetCode】091. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- Java for LeetCode 091 Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- LeetCode之“动态规划”:Decode Ways
题目链接 题目要求: A message containing letters from A-Z is being encoded to numbers using the following map ...
- [leetcode.com]算法题目 - Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- LeetCode(91) Decode Ways
题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...
- 091 Decode Ways 解码方法
包含 A-Z 的字母的消息通过以下规则编码:'A' -> 1'B' -> 2...'Z' -> 26给定一个包含数字的编码消息,请确定解码方法的总数.例如,给定消息为 "1 ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
随机推荐
- BZOJ3672 : [Noi2014]购票
设d[i]表示i到1的距离 f[i]=w[i]+min(f[j]+(d[i]-d[j])*v[i])=w[i]+d[i]*v[i]+min(-d[j]*v[i]+f[j]) 对这棵树进行点分治,每次递 ...
- silverlight 跨域访问 wcf
先介绍一下我的测试项目,我用flash和silverlight一同来调用一个webservice,一个flash客户端,一个silverlight客户端,一个web项目来host flash和silv ...
- 【wikioi】2495 水叮当的舞步(IDA*)
http://wikioi.com/problem/2495/ 这题我还是看题解啊囧.(搜索实在太弱.完全没想到A*,还有看题的时候想错了,.,- -) 好吧,估价还是那么的简单,判断颜色不同的数目即 ...
- Using pg_dump restore dump file on Odoo
pg_restore -C -d postgres db.dump
- #ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #endif
这样的代码到底是什么意思呢?首先,__cplusplus是cpp中的自定义宏,那么定义了这个宏的话表示这是一段cpp的代码,也就是说,上面的代码的含义是:如果这是一段cpp的代码,那么加入" ...
- IOS第四天(5:创建备份区按钮和判断结果)
创建备份区按钮和判断结果 /** 创建备选区按钮 */ - (void)createOptionButtons:(HMQuestion *)question { // 问题:每次调用下一题方法时,都会 ...
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- ORA-12518: TNS: 监听程序无法分发客户机连接
在团队成员增多时,经常出现“无法分发客户端连接”等问题.在网上搜索一番后,最终解决了该问题,现将解决方案总结如下,以供参考和以后备用. 原因:团队成员增多,原有数据库设置不够用,导致连接plsql和启 ...
- git命令常见问题总结
1.git如何放弃所有本地修改 git checkout . #本地所有修改的.没有的提交的,都返回到原来的状态 git stash #把所有没有提交的修改暂存到stash里面.可用git stash ...
- 1st-code-review summary
每次做code review,先贤谆谆教诲便在耳畔响起: "There are only two hard problems in Computer Science: cache inval ...