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,1,然后由0,1计算得到后面的数·······

class Solution {
public:
int numDecodings(string s) {
int len=s.size();
if(len==||s[]=='')
return ;
int num;
int res;
if(len==)
num =s[]-'';
else if(len==)
num=*(s[]-'')+s[]-'';
if(num==)
return ;
else if(num<=)
return ;
else if(num>&&num<=)
return ;
else if(num>&&num<=)
return ;
res=max(numDecodings(s.substr(,))+numDecodings(s.substr(,len)),
numDecodings(s.substr(,))+numDecodings(s.substr(,len)));
return res;
}
};

正确的做法应该是这样的:

class Solution {
public:
int numDecodings(string s) {
int len=s.size();
if(len==)
return ;
else if(len==)
return s[]!=''?:;
else if(len==)
return (s[]!=''&&s[]!=''?:)+(s[]!=''&&((*(s[]-'')+s[]-'')<=)?:); int* flag=new int[len];
flag[]=s[]!=''?:;
flag[]=(s[]!=''&&s[]!=''?:)+(s[]!=''&&((*(s[]-'')+s[]-'')<=)?:);
for(int i=;i<len;i++)
{
flag[i]=;
if(s[i]!='')
{
flag[i]+=flag[i-];
}
if(s[i-]!=''&&(*(s[i-]-'')+s[i]-''<=))
{
flag[i]+=flag[i-];
}
}
int res=flag[len-];
delete []flag;
return res;
}
};

Decode Ways——动态规划的更多相关文章

  1. decode ways(动态规划)

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

  2. 91. Decode Ways(动态规划 26个字母解码个数)

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

  3. 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways

    引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...

  4. Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

    Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...

  5. [LeetCode] Decode Ways 解码方法

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

  6. 44. Decode Ways && Gray Code

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

  7. leetcode面试准备:Decode Ways

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

  8. Decode Ways leetcode java

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

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

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

随机推荐

  1. SpringMVC源码解析-DispatcherServlet启动流程和初始化

    在使用springmvc框架,会在web.xml文件配置一个DispatcherServlet,这正是web容器开始初始化,同时会在建立自己的上下文来持有SpringMVC的bean对象. 先从Dis ...

  2. getQueryString----获取url中得参数

    /** * 获取url中得参数 * @param name * @returns {null} */ window.getQueryString = function (name) { var reg ...

  3. Nginx的启动、停止、平滑重启

    转载自:http://www.xj123.info/2572.html 启动Nginx /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/ngi ...

  4. 【Android】完善Android学习(一:API 2.3.3)

    备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...

  5. LightOJ 1218 概率水题(几何分布)

    题意:给你一个n面骰子,问你投出所有面需要的次数的期望值是多少. 题解:放在过去估计秒解,结果现在自己想好久,还查了下,有人用极限证明...实际上仔细想想这种情况投出与前面不一样的概率p的倒数就是次数 ...

  6. springcloud(一):大话Spring Cloud(山东数漫江湖)

    研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...

  7. 多种方法过Codeforces Round #270的A题(奇偶法、打表法和Miller_Rabin(这个方法才是重点))

    题目链接:http://codeforces.com/contest/472/problem/A 题目: 题意:哥德巴赫猜想是:一个大于2的素数一定可以表示为两个素数的和.此题则是将其修改为:一个大于 ...

  8. python keras YOLOv3实现目标检测

    1.连接 https://www.jianshu.com/p/3943be47fe84

  9. Android Studio 中引入Library

    启动AndroidStudio后,打开你需要接收Library的项目.比如有两个项目,项目A,和Library项目B,那么打开项目A.图中所示为项目的结构图,点击右上角的File菜单. 2 在下拉菜单 ...

  10. git 配置多用户

    .ssh 下的 config.txt 内容 # 配置github.com Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa_ ...