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' - ...
随机推荐
- python基础之魔法方法
由于hexo自带的markdown渲染引擎对双下划线做了转义,在正文中看到的魔法方法前后都没有双下划线 setattr.getattr.delattr 可以拦截对对象属性的访问 setattr函数是用 ...
- c++ linux 判断string是中文的 or 英文的 字符串。
#include <iostream> #include <string.h> #include <stdio.h> #include <stdlib.h&g ...
- 中国MOOC_面向对象程序设计——Java语言_第2周 对象交互_秒计时的数字时钟
第2周编程题 查看帮助 返回 第2周编程题,在课程所给的时钟程序的基础上修改 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后由系 ...
- c#知识梳理
转:http://www.cnblogs.com/zhouzhou-aspnet/articles/2591596.html 本文是一个菜鸟所写,本文面向的人群就是像我这样的小菜鸟,工作一年也辛辛苦苦 ...
- Rsync+inotify自动同步数据
一.简介 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足. 首先,rsync在同步数据时,需要扫描所有文件后进行比对,进行差量传 ...
- MongoDB入门(5)- 我们自己封装的MongoDB-Java版本
用法 实体定义 package com.wisdombud.mongotool; import java.io.Serializable; import java.util.Date; import ...
- [洛谷P1707] 刷题比赛
洛谷题目连接:刷题比赛 题目背景 nodgd是一个喜欢写程序的同学,前不久洛谷OJ横空出世,nodgd同学当然第一时间来到洛谷OJ刷题.于是发生了一系列有趣的事情,他就打算用这些事情来出题恶心大家-- ...
- python实现堆栈、队列
一.利用python列表实现堆栈和队列 堆栈: 堆栈是一个后进先出的数据结构,其工作方式就像生活中常见到的直梯,先进去的人肯定是最后出. 我们可以设置一个类,用列表来存放栈中的元素的信息,利用列表的a ...
- 【BZOJ1560】【JSOI2009】火星藏宝图 [DP]
火星藏宝图 Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description Input Output Samp ...
- c语言目录操作总结
=================================================== char *getcwd( char *buffer, int maxlen ); (获取当前目 ...