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. [BZOJ1131/POI2008]Sta树的深度

    Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. Output ...

  2. 【神仙题】【P4885】 灭顶之灾

    传送门 Description 请将题目名称的首字母连起来读 Scarlet有一张$n*m$的神秘表格.现在Scarlet向表格中填数字,她会从第一行中的某个格子起,按照从左往右,从上往下的顺序依次填 ...

  3. MFC单文档多视图程序设计与Splitter拆分窗口

    1. 创建不同的子frame. 在文档视图程序中 CMainFrame(class CMainFrame : public CMDIFrameWndEx) 继承自 CMDIFrameWnd (CMDI ...

  4. bzoj 1123 [POI2008]BLO Tarjan求割点

    [POI2008]BLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1540  Solved: 711[Submit][Status][Discu ...

  5. vue-transition-move

    <!Doctype> <html> <head> <meta charset="utf-8"> <meta name=&quo ...

  6. 51Nod 1080

    #include "bits/stdc++.h" using namespace std; #define LL long long #define INF 0x3f3f3f3f3 ...

  7. MyBatis框架的使用及源码分析(八) MapperMethod

    从 <MyBatis框架中Mapper映射配置的使用及原理解析(七) MapperProxy,MapperProxyFactory> 文中,我们知道Mapper,通过MapperProxy ...

  8. Eclipse导入Java工程导入错误

    1.在一台电脑A上用Eclipse新建的Java工程,换了一台电脑B,再用Eclipse导入时却出现了错误,工程显示红色叹号,并有如下两个错误提示: The project cannot be bui ...

  9. RDLC - 后台代码直接导出Excel/PDF/Word格式

    最近做报表功能,用到了.net的报表组件rdlc. 其中有个功能就是后台代码直接输出Excel/PDF/Word格式的文件,网上看了些资源,做个总结: 参考地址 我直接贴出代码: //自动导出exce ...

  10. 招人不是HR第一职责,留住人才是

    什么是HR的第一责任?我希望你们知道,招人不是你的第一职责,留住人才是你的第一职责.HR工作是相当难做的,你们是公司集团内里各个部门中最难做的部门,也是最具战略性的部门. 以人为本,这个“人”就是阿里 ...