Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
题目描述
一条报文包含字母A-Z,使用下面的字母-数字映射进行解码
'A' -> 1
'B' -> 2
...
'Z' -> 26
给一串包含数字的加密报文,求有多少种解码方式
举个例子,已知报文"12",它可以解码为AB(1 2),也可以是L (12)
所以解码方式有2种。
测试样例
Input:
"0"
"121212"
"101010"
"1001"
"0101"
Output:
0
13
1
0
0
详细分析
这道题不难,不过corner cases比较多,需要仔细分析。先考虑1212这个例子:(为了表达方便,我们用逗号分隔表示每种解码方式而不用扳手指算,比如1212的一种解码方式为12,12而不用L,L)
1=>
1
12=>
1,2
12
121=>
1,2,1
1,21
12,1
1212=>
1,2,1,2
1,21,2
12,1,2
1,2,12
12,12
到这里就可以总结出规律了,对于1212,其实是两种解码的和:
1,2,1,(2)
1,21,(2)
12,1,(2)
-----------
1,2,(12)
12,(12)
分割线上面是121的解码方式,并在后加以当前下标的2,分割线下面是12的解码方式加以当前下标和前一个下标表示的字符。
可以看出,如果当前字符和前面一个字符可以构成>10 && <=26(不包括20,至于为什么等下说)的字符,那么当前解码方式就是:
dp[i]=dp[i-1]+dp[i-2]
现在考虑一些corner case,如果当前字符是0,那么它并不符合上面的递推公式,考虑2020:
20=>
20
202=>
20,2
2020=>
20,(20)
可以看到2020,由于0不在解码范围内,所以它不能与前一项通过添加后缀的方式构成解码方式,它只是简单等于前两项然后加上后缀20,同理还有10。
按照这种思路,我们可以得出下面的状态转移:
let x = s.substr(i-1,2);
x>0 && x<10: dp[i]=dp[i-1]
x==10: dp[i]=dp[i-2]
x>10&&x<20: dp[i]=dp[i-1]+dp[i-2]
x==20: dp[i]=dp[i-2]
x>20&&x<=26: dp[i]=dp[i-1]+dp[i-2]
x>26&&x%10!=0: dp[i]=dp[i-1];
x>26&&x%10==0: return 0
代码实现
代码太烂凑合看吧...
class Solution {
public:
int numDecodings(string s) {
if(s.length()==0){
return 0;
}
if(s[0]=='0'){
return 0;
}
if(s.length()==1){
return 1;
}
int dp[100000];
dp[0]=1;
std::string ns = s.substr(0,2);
int t = atoi(ns.c_str());
if(t>0 && t<10){
return 0;
}else if(t==10){
dp[1]=1;
}else if(t>10 && t<20){
dp[1]=2;
}else if(t==20){
dp[1]=1;
}else if(t>20 && t<=26){
dp[1]=2;
}else if(t>26 && t%10!=0){
dp[1]=1;
}else{
return 0;
}
if(s.length()==2){
return dp[1];
}
for(int i=2;i<s.length();i++){
std::string tempStr = s.substr(i-1,2);
int n = atoi(tempStr.c_str());
if((n>26 && n%10!=0)||(n>0 && n<10)){
dp[i]=dp[i-1];
}else if(n>10 && n<=26&& n!=20){
dp[i]=dp[i-1]+dp[i-2];
}else if(n==10 || n==20){
dp[i]=dp[i-2];
}else if(n==0 || n%10==0){
return 0;
}
}
return dp[s.length()-1];
}
};
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)的更多相关文章
- [LeetCode] 91. 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' - ...
- leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- leetcode 91 Decode Ways I
令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...
- [LeetCode] 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 mapping: 'A' - ...
- [LintCode] 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 ...
- Leetcode91.Decode Ways解码方法
一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...
随机推荐
- FFMPEG: avformat_find_stream_info()函数
av_find_stream_info()中是要不断的读取数据包,解码获得相应的信息 其中: st->codec->codec_type:0:视频,1:音频,2:数据 st->cod ...
- 类型:。net;问题:HQL;结果:HQL: Hibernate查询语言
HQL: Hibernate查询语言 Hibernate配备了一种非常强大的查询语言,这种语言看上去很像SQL.但是不要被语法结构 上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它可 ...
- 《Android应用性能优化》 第4章 高效使用内存
本地类型 大小 字节 boolean jboolean 8位(取决于VM) 1 byte jbyte 8位 1 char jchar 16位 2 short jshort 16位 2 int ji ...
- IE双边距bug
标准参考 根据 W3C CSS2.1 规范中的描述,对于非替换的浮动元素,若 'margin-left' 或 'margin-right' 特性的计算值为 'auto',则它们的实际使用值为 '0'. ...
- sql 一些偶尔会用到的写法和函数 不定时更新
小数转整数: --round() 遵循四舍五入把原值转化为指定小数位数,如: ) -- =1 ) -- =2 --floor() 向下舍入为指定小数位数 如: SELECT floor(1.45) - ...
- ROS Learning-021 learning_tf-05(编程) now() 和 Time(0) 的区别 (Python版)
ROS Indigo learning_tf-05 now() 和 Time(0)的区别 (Python版) - waitForTransform() 函数 我使用的虚拟机软件:VMware Work ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- ZROI2018提高day3t1
传送门 分析 我们可以用贪心的思想.对于所有并没有指明关系的数一定是将小的放在前面.于是我们按顺序在每一个已经指明大小顺序的数前面插入所有比它小且没有指明关系的数.详见代码. 代码 #include& ...
- 5.Qt model view设计模式
Introduction to Model/View Programming QT4 介绍了一系列新的 Item View 类,这些类使用Model/View结构来管理数据和数据如何呈现给 ...
- JavaWeb_泛型(Generic)
JDK5以前,对象保存到集合中就会失去其特性,取出时通常要程序员手工进行类型的强制转换,这样不可避免的就会引发程序的一些安全性问题.例如: ArrayList list = new ArrayList ...