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.

问题:对给定纯数字字符串解码,求解法的可能情况有多少种。

注:0 开头的字符串算作无效, 例如, "02" 无法解码。

也是一道蛮典型的 DP 问题。

总的解码个数,等于对第一个数字单独解码的情况,加上对前两个数字一起解码的情况。

利用 vector<int> v 记录中间计算结果。 v[i] 表示 s[i...n] 的解码个数。

     vector<int> v;

     int decode(string s, int p){

         if (v[p] != -) {
return v[p];
} if (s[] == '') {
v[p] = ;
return ;
} if (s.size() == ) {
v[p] = ;
return ;
} if (s.size() == ) {
int tmp = ;
if (s[] != '') {
tmp++;
} if (stoi(s) <= ) {
tmp++;
}
v[p] = tmp;
return tmp;
} int n1 = ;
if (s[] != '') {
n1 = decode(s.substr(), p+);
} int n2 = ;
if (stoi(s.substr(, )) <= ) {
n2 = decode(s.substr(), p+);
} int res = n1 + n2; v[p] = res;
return res;
} int numDecodings(string s) {
vector<int> tmp(s.size() , -); v = tmp; if(s.size() == ){
return ;
} int res = decode(s, ); return res;
}

这道题解了两次,第一次没有解出来,放在一边,做了其他 DP 题目后,第二次再做,觉得顺畅了许多。

第一次解的时候,思路是分治(Divide and conquer)。

分治主要有三个步骤:

分(Divide) :将原问题分割为类型相同的子问题。

治(Conquer) :分别解决各个子问题

整合(Combine) : 将各个子问题结果整合,得到原问题解。

算法: 将密码字符串从中间分割,分别求解两个子问题,并求解中间不分割的情况,整合三个结果,得到原问题的解。

但是,求解中间不分割情况处理起来比较复杂,就没有继续下去。

第二次解的时候,已经做了一些 DP 问题,思路就往 DP 方向想。

动态规划(Dynamic Programming) 的关键条件有两个:

重叠子问题(overlapping sub-problems) : 子问题和原问题是同类型,解法可重用。这点和分治的前两个步骤类似。

最优子结构(optimal substructure) :子问题的结果,可以比较直接地得到原问题的结果。

对于字符串问题,

若采用分治思路,左右子问题都必须求解,并且还要考虑中间不分割的情况,算法容易变得复杂。

若采用 DP 思路,从左往右一直扫过去,免去了分治中求整合的那一步骤,时间复杂度和分治应该差不多,不过算法实现更加简洁。

[LeetCode] Decode Ways 解题思路的更多相关文章

  1. LeetCode:Decode Ways 解题报告

    Decode WaysA message containing letters from A-Z is being encoded to numbers using the following map ...

  2. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  3. [leetcode]Decode Ways @ Python

    原题地址:https://oj.leetcode.com/problems/decode-ways/ 题意: A message containing letters from A-Z is bein ...

  4. [LeetCode] Decode Ways 解码方法

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

  5. [LeetCode] Decode Ways [33]

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

  6. [LeetCode] Decode Ways II 解码方法之二

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

  7. [LeetCode] Word Break 解题思路

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  8. [LeetCode] Maximum Gap 解题思路

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  9. [LeetCode] decode ways 解码方式

    A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...

随机推荐

  1. Nginx中让 重写后的路径 自动增加斜线 /

    http://www.111cn.net/sys/nginx/56067.htm(参考文章) 现在有个这样的需求,在重写的url地址后,自动加斜线 / 例如 xx.com/abc/1-2  (默认ur ...

  2. get share button count

    class shareCount { private $url,$timeout; function __construct($url,$timeout=10) { $this->url=raw ...

  3. 那些所谓过滤掉iOS菜鸟的面试题

    一.struct和class的区别<swfit里的array是什么类型,在大量复制时会不会有性能问题.> class是引用类型,struct是值类型 class可以继承类.接口和被继承,s ...

  4. tmux复制到windows剪贴板/粘贴板的坑

    以下所有操作都是在windows下面用putty连接linux centos6的情景下. 一直很纳闷为什么在tmux模式下不能把复制到的文字放到系统的粘贴板里面呢?通过层层阻碍,终于找到了原因. 去掉 ...

  5. session 保存在指定的数据表,cookie设置

    首先建立数据表,可在ThinkPHP/Extend/Driver/Session/SessionDb.class.php中copy代码 在配置文件中配置: 'SESSION_TYPE' => ' ...

  6. PHP异常处理

    一.异常处理——可以有效地控制多条出现错误或异常的代码 基本语法如下: try{ //可能出现异常的代码 } catch(Exception $e){ //对异常处理 //1.自己处理 //2.不作处 ...

  7. php发送http请求

    http请求有get,post. php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]. file_get_contents();详情见:http://www.cnblogs.com/ ...

  8. 在ADS上由于volatile惹得祸

    C语言关键字volatile是一个危险的东东,笔者再用ADS做S3C2440定时器中断实验就因为这个关键字出了错.出现错误情况的准确描述是:定义一个变量时没有用volatile关键字,而且紧接着whi ...

  9. vs2013调试崩溃,重启电脑依旧崩溃

    如果大家遇到 VS断点调试程序崩溃的问题,可以排查是不是有这个问题 VSx新安装了插件 点击工具---扩展和更新  禁用最新安装的程序 一般就没有问题了

  10. VCC,VDD,VEE,VSS,VPP 表示的意义

    转自VCC,VDD,VEE,VSS,VPP 表示的意义 VCC,VDD,VEE,VSS,VPP 表示的意义 版本一: 简单说来,可以这样理解: 一.解释 VCC:C=circuit 表示电路的意思, ...