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 ...
随机推荐
- iOS开发之---KVC全解
————————————————————————————————————————————————————————————— 一 KVC的基本概念 KVC是Key Value Coding的缩写,意思是 ...
- c语言-构建一个静态二叉树
第一.树的构建 定义树结构 struct BTNode { char data; struct BTNode* pLChild; struct BTNode* pRChild; }; 静态方式创建一个 ...
- js笔试题一套(未完待续)
1.下面程序的运行结果是: function test(x, y, z) { alert(test.length); alert(arguments.length); alert(arguments. ...
- 关于更新pip的心得
如果pip install --upgrade pip 删除了自己,但是无法安装新的自己. 那么下载最新的pip,解压 1.在命令窗口输入 python(前提条件已经在系统路径) setup.py ...
- eclipse利用mybatis-generator生成代码
由于mybatis是半自动的ORM框架,表到POJO的映射可以由mybatis-generator完成,映射文件也可以由它生成,下面介绍生成步骤: 1.新建maven项目:File->Other ...
- awk简要使用
1 前言 awk是Unix环境下一种非常好的语言,适合于文本处理和报表生成,它还有许多精心设计的特性,允许进行特殊技巧程序设计.对于短消息来说,比如处理话单文件,使用awk就非常方便 ...
- ActionbarActivity和普通的Activity的区别
ActionbarActivity用于支持API11以下的程序支持ActionBar的功能,但是需要加入支持库 不这样做的话只有API11以上的才能支持ActionBar
- eclipse java 注释模板配置详解
设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元 ...
- Tensorflow fetch和feed
import tensorflow as tf #Fetch input1 = tf.constant(1.0)input2 = tf.constant(3.0)input3 = tf.constan ...
- php学习笔记-for循环
for(init;condition;statement) { func(); } for循环的执行逻辑是先执行一次init语句,然后判断condition是否为true,是则执行func(),再执行 ...