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 解码方法(动态规划,字符串处理)的更多相关文章

  1. [LeetCode] 91. Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  2. [leetcode]91. Decode Ways解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  3. leetcode@ [91] Decode Ways (Dynamic Programming)

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  4. 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 ...

  5. [LeetCode] Decode Ways 解码方法个数、动态规划

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  6. [LeetCode] Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  7. [LintCode] Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  8. 091 Decode Ways 解码方法

    包含 A-Z 的字母的消息通过以下规则编码:'A' -> 1'B' -> 2...'Z' -> 26给定一个包含数字的编码消息,请确定解码方法的总数.例如,给定消息为 "1 ...

  9. Leetcode91.Decode Ways解码方法

    一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...

随机推荐

  1. SUSE eth0 No such device

    删除 etc/udev/rules.d/70-persistent-net.rules 文件  之后重启让系统重新生成eth0配置文件 rm -f etc/udev/rules.d/70-persis ...

  2. oracle connect by用法篇 (包括树遍历)之一

    1.基本语法 select * from table [start with condition1] connect by [prior] id=parentid 一般用来查找存在父子关系的数据,也就 ...

  3. linux命令-fdisk分区

    fdisk -l   查看分区状况,也可查看指定分区 Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 26 ...

  4. CSS3 高级属性

    尽管现代浏览器已经支持了众多的CSS3属性,但是大部分设计师和开发人员貌似依然在关注于一些很“主流”的属性,如border-radius.box-shadow或者transform等.它们有良好的文档 ...

  5. Hbase表重命名 表改名

    PS:现在我有个表 :test11_new  ,我要给他改名 开始: 1.先disable掉表hbase(main):023:0> disable 'test11_new' 0 row(s) i ...

  6. mysql存储过程@命名变量的区别

    存储过程中@是用来标识每一次运行存储过程都会保存的值.而直接命名是每一次都会初始化的局部变量.@@是用来标识全局变量. CREATE PROCEDURE prc_test ()BEGIN DECLAR ...

  7. junit4新框架hamcrest的assertThat

    assertThat JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活 /**equalTo匹配符断言被测的tes ...

  8. WPA密码攻击宝典

    原则:密码以8-10位为主.11位仅限于当地手机号.一般人的多年用数字做密码的习惯和心理,先数 字.再字母,或数字.字母重复几遍,字符几乎全用小写,所以淘汰大写及"~!@#$%^&* ...

  9. 关于stickybroadcast

    stickybroadcast顾名思义,粘性广播,从字面上我们可以联想到service的返回值中也有个一stick,在service中stick作用是当返回了之后服务被杀死,会重启服务. 但是这里的s ...

  10. SpringJdbc 【springjdbc的使用方法】

    1 什么是springjdbc spring对jdbc的封装 2 使用SpringJdbc的编程步骤 2.1 导包 spring-jdbc : springjdbc的包 mysql : MySQL的驱 ...