[leetcode.com]算法题目 - 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.
class Solution {
public:
int numDecodings(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
}
};
答案模板
本题情况很繁琐,尝试了好久才通过测试。注意“012”这样以零开头的string,number of ways 是0。代码如下:
class Solution {
public:
int numDecodings(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = s.length();
if (==len || ''==s.at()) return ;
if (==len) return ;
if (==len){
int t1 = s.at()-'';
int t2 = s.at()-'';
t2 += t1*;
if( == t2 || == t2)
return ;
else if(t2<=)
return ;
else if(==t2%)
return ;
else
return ;
}
int *record = new int[len];
record[]=numDecodings(s.substr(len-,));
record[]=numDecodings(s.substr(len-,));
for(int k=;k<len;k++){
string s_string = s.substr(len-k-,k+);
int a = s_string.at()-'';
if (==a)
record[k]=;
else if (a>)
record[k]= record[k-];
else if (==a)
record[k]= record[k-]+record[k-];
else // (2==a)
{
int kk = s_string.at()-'';
if(kk>)
record[k]= record[k-];
else
record[k]= record[k-]+record[k-];
}
}
int result = record[len-];
delete[] record;
return result;
}
};
我的答案
注意分析其中的每一种情况,必须要都考虑周全。用record数组记录已经计算过的数据,避免用递归所产生的重复计算。
[leetcode.com]算法题目 - Decode Ways的更多相关文章
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- LeetCode之“动态规划”:Decode Ways
题目链接 题目要求: A message containing letters from A-Z is being encoded to numbers using the following map ...
- LeetCode(91) Decode Ways
题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode.com]算法题目 - Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode.com]算法题目 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode.com]算法题目 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [leetcode.com]算法题目 - Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- ViewPager源码分析——滑动切换页面处理过程
上周客户反馈Contacts快速滑动界面切换tab有明显卡顿,让优化. 自己验证又没发现卡顿现象,但总得给客户一个技术性的回复,于是看了一下ViewPager源码中处理滑动切换tab的过程. View ...
- 解决SecureCRT超时自动断开的问题
http://blog.csdn.net/hcwzq/article/details/7944941. http://discuzx.sinaapp.com/mediawiki-chapter.htm ...
- tms web core 里面调用pascal 过程。
procedure show(s:string);begin showmessage(s);end; procedure TForm3.WebButton1Click(Sender: TObject ...
- Maven手动将jar包放入本地仓库
mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面 ...
- Rest架构风格的实践(使用通用Mapper技术)
1.根据用户 id 查询用户数据 1.1 controll控制器 @RequestMapping("restful/user") @Controller public class ...
- Mat补充
Mat的创建 1.使用Mat的构造函数 Mat test(2,2,CV_8UC3,Scalar(0,0,255)); 2.使用Mat的构造函数 int sizes[3] = {2,2,2}; Mat ...
- 安卓开机logo和开机动画的几种实现方法
安卓4.2可用方法2-4,第一种方法未验证. 从理论上来说,android 有4个开机启动画面. 第一个应该是U-BOOT的启动画面,有些设备为了满足按动电源即有显示,在UBOOT里加了开机画面,实现 ...
- 微信小程序两种滑动方式
竖向滑动: <scroll-view scroll-y="true" style="height: 200rpx;"> <view style ...
- 高性能高可用的分布式唯一ID服务——mooon-uniq-id
目录 目录 1 1. 前言 1 2. 名词 1 3. 功能 1 4. 唯一性原理 2 5. 系统结构 2 5.1. mooon-uniq-agent 2 5.2. mooon-uniq-master ...
- 进程之multiprocessing
进程的状态: