Decode Ways——动态规划
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——动态规划的更多相关文章
- decode ways(动态规划)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 91. Decode Ways(动态规划 26个字母解码个数)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 44. Decode Ways && Gray Code
Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...
- leetcode面试准备:Decode Ways
1 题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: ...
- Decode Ways leetcode java
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
随机推荐
- bzoj1656: [Usaco2006 Jan] The Grove 树木 (bfs+新姿势)
题目大意:一个n*m的图中,“.”可走,“X”不可走,“*”为起点,问从起点开始绕所有X一圈回到起点最少需要走多少步. 一开始看到这题,自己脑洞了下怎么写,应该是可过,然后跑去看了题解,又学会了一 ...
- maven根据不同的运行环境,打包不同的配置文件(转载)
使用maven管理项目中的依赖,非常的方便.同时利用maven内置的各种插件,在命令行模式下完成打包.部署等操作,可方便后期的持续集成使用. 但是每一个maven工程(比如web项目),开发人员在开发 ...
- best code #54 div 2 A 水
A problem of sorting Accepts: 443 Submissions: 1696 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- css渐变知识知多少
<!DOCTYPE html><html><head><meta charset="utf-8"> <title>教程( ...
- 数据结构&图论:LCT
HDU4010 类比静态区间问题->动态区间问题的拓展 我们这里把区间变成树,树上的写改删查问题,最最最常用LCT解决 LCT用来维护动态的森林,对于森林中的每一棵树,用Splay维护. LCT ...
- LightOJ 1129 - Consistency Checker Trie树模板
题意:给出n条串判断是否存在一个串为另一个串的前缀. 思路:套Trie树的模板,先全部插入,再查找每个字串,如果查找字串完毕,但还存在下一个节点,说明存在前缀. /** @Date : 2016-11 ...
- LightOJ 1306 - Solutions to an Equation 裸EXGCD
本题是极其裸的EXGCD AX+BY+C=0 给你a b c 和x与y的区间范围,问你整数解有几组 作为EXGCD入门,题目比较简单 主要需要考虑区间范围的向上.向下取整,及正负符号的问题 问题是这正 ...
- WPF 添加Adminstrator 权限
在WPF应用开发中,需要WPF操作后台注册的Windows Service,可是WIX打包的安装程序不具备赋予WPF App默认管理员权限. 因此,需要我们手工在WPF项目中添加管理员权限: 1.右击 ...
- uoj308 【UNR #2】UOJ拯救计划
传送门:http://uoj.ac/problem/308 [题解] 考虑枚举用了$i$所学校,那么贡献为${k \choose i} * cnt * i!$ 意思是从$k$所选$i$所出来染色,$c ...
- 【NOIP】提高组2012 疫情控制
[题意]n个点的树,1为根,要求删除一些点使得截断根节点和所有叶子结点的路径(不能删根,可以删叶子).有m支军队在m个点上,每时刻所有军队可以走一步,最终走到的地方就是删除的点,求最短时间. [算法] ...