[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: ' ...
随机推荐
- JSTL 的 if else : 有 c:if 没有 else 的处理
jstl的c:if 没有else 想要实现if...else...: 可以用下面的解决 -------------------------------------------------------- ...
- CentOS6.4 配置Tengine
1.安装Nginx所需的pcre-devel库 yum install -y gcc gcc-c++ wget ftp://ftp.csx.cam.ac.uk/pub/software/program ...
- 【BZOJ】1049: [HAOI2006]数字序列(lis+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1049 题意:给一个长度为n的整数序列.把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希 ...
- 【BZOJ】3289: Mato的文件管理(莫队算法+树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=3289 很裸的莫队... 离线了区间然后分块排序后,询问时搞搞就行了. 本题中,如果知道$[l, r] ...
- SQL Server 中 with tmp 临时表的用法
SQL Server 中 with tmp 临时表的用法 ----------with临时表用法,有时候采用临时表比采用in的效率更高,避免了全表扫描. 实例中实现了查询普通题.大题.子题目的sql ...
- 李洪强-C语言7-C语言运算符
C语言运算符 一.算术运算 C语言一共有34种运算符,包括常见的加减乘除运算. ①. 加法:+ 还可以表示正号 ②. 减法:- 还可以表示负号 ③. 乘法:* 非数学意义上的X ④. 除法:/ 注意 ...
- JavaScript - prototype 和 call 的理解
prototype: 其实对象直接通过 [object.属性/方法] 来定义方法和属性也是可以的,但是会变成静态属性和静态方法,也就是调用的时候也是通过“.”来调用的.但如果通过prototype来 ...
- ci文件缓存使用,可以用来实现多模板切换 改写URL辅助函数
//2015年2月28日15:13:41 $this->load->driver('cache', array('adapter' => 'file'));//加载适配器 //请注意 ...
- Eclipse学习记录
设置背景色:http://jingyan.baidu.com/article/2a138328b5d9ea074a134fc7.html 项目文件说明:http://www.cnblogs.com/p ...
- IE6 — 你若安好,便是晴天霹雳 [ 乱弹 ]
为什么还有人在用IE6?估计和中国的盗版业有很大关系吧.小白的电脑启不来了,请人重装系统,一张古老的Ghost搞定,IE6便落地生根.今天碰到一例报告说某网站在IE6下丑陋吓人,无心无力去解决,于是来 ...