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' - ...
随机推荐
- [学习笔记]min-max容斥
[Learning]min-max容斥以及推广 min-max容斥 就是max(a,b)=min(a)+min(b)-min(a,b) max(a,b,c)=a+b+c-min(a,b)-min(a, ...
- YBT 5.3 数位动态规划
记忆化搜索的专题 题解在代码中 Amount of Degrees[loj 10163] /* 此题可以转换成将10进制转成b进制后有k个1其他都为0的个数 所以用记忆化dfs dp[pos][sum ...
- git使用笔记(十一)rebase
By francis_hao Oct 22,2017 git-rebase,改变commit的基础参照 概要 git rebase [-i | --interactive] [options ...
- Good Bye 2015 C
C. New Year and Domino time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- bzoj 3212 Pku3468 A Simple Problem with Integers 线段树基本操作
Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2173 Solved: ...
- POJ 3984 BFS
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20665 Accepted: 12100 Descriptio ...
- Flume 入门--几种不同的Sinks
主要介绍几种常见Flume的Sink--汇聚点 1.Logger Sink 记录INFO级别的日志,一般用于调试.前面介绍Source时候用到的Sink都是这个类型的Sink 必须配置的属性: 属性说 ...
- 【Dream Counting, 2006 Dec-数数的梦】数位dp
题意:给定两个数,问区间[A,B]中0~9分别出现了多少次.A,B<=10^18 题解:应该是最裸的数位dp吧..一开始没有记忆化tle了TAT 我们可以求出区间[0,B]的,再减去区间[0,A ...
- ...args剩余参数用法
剩余参数语法允许我们将一个不定数量的参数表示为一个数组. function sum(...theArgs) { return theArgs.reduce((previous, current) ...
- perl6 修改文件并覆盖
use v6; my $filename = 'data.txt'; my $data = slurp $filename; say $data; $data ~~ s/'4'/'ABC'/; say ...