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. [vijos1246]文科生的悲哀(二) 动态规划

    背景 化学不及格的Matrix67无奈选择了文科.他必须硬着头皮艰难地进行着文科的学习. 描述 这学期的政治.历史和地理课本各有n章.每一科的教学必须按章节从前往后依次进行.若干章政治.若干章历史和若 ...

  2. leetcode590

    树的后序遍历. class Solution { public: vector<Node> Tree; void postTree(Node node) { for (auto n : n ...

  3. LAMP 2.9 php扩展模块如何安装

    php 和 apache 类似,核心文件为/usr/local/php/bin/php,针对 apache 的是/usr/local/apache2/modules/libphp5.so 模块.这两个 ...

  4. Ubuntu安装Chrome及hosts修改

    Ubuntu16.04 1.chrome安装 获取安装包http://www.google.cn/chrome/browser/desktop/index.html 在安装包目录打开终端执行sudo  ...

  5. maven 配置说明

    1.坐标 1.1 每一jar文件都有一个唯一坐标.通过坐标可以精确确定是哪个jar 1.2 坐标组成 1.2.1 Group ID : 公司名.公司网址倒写 1.2.2 Artifact ID : 项 ...

  6. Absolute Layout

    ----------------siwuxie095 根面板 contentPane 的默认布局为 Border Layout,将其 切换为 Absolute Layout Absolute Layo ...

  7. C++面向对象类的实例题目八

    题目描述: 编写一个程序输入3个学生的英语和计算机成绩,并按照总分从高到低排序.要求设计一个学生类Student,其定义如下: 程序代码: #include<iostream> using ...

  8. mac上virtualBox的安装和使用

    一.下载和安装 去oracle官网下载mac版的virtualBox. 官网下载地址:https://www.virtualbox.org/. 下载好后按照向导进行安装即可. 二.使用方法 1.新建虚 ...

  9. MySQL update select组合

    update t_news inner join (select readCount from t_news t2 where t2.id=1) t1 set t_news.readCount = t ...

  10. Windows中lib和DLL区别和使用

    本文转载自:http://www.cppblog.com/amazon/archive/2009/09/04/95318.html 共有两种库:一种是LIB包含了函数所在的DLL文件和文件中函数位置的 ...