[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 ...
随机推荐
- eclipse构建maven的web项目(转载)
eclipse构建maven的web项目 分类: java opensource2013-12-25 16:22 43人阅读 评论(0) 收藏 举报 maven框架webappwebeclipse 使 ...
- mysql安装后初始密码
在安装过程中没有任何提示,安装完之后无法登陆 后经查询发现,可以暂时以 mysql -u root -p登陆 此账户没有密码直接enter即可. update user set Password=PA ...
- centos 7 搭建pip源
一.安装pip2pi工具: pip install pip2pi 或编译: git clone https://github.com/wolever/pip2pi cd pip2pi python s ...
- 社区发现(Community Detection)算法(转)
作者: peghoty 出处: http://blog.csdn.net/peghoty/article/details/9286905 社区发现(Community Detection)算法用来发现 ...
- Idea中如何将web项目打包成war包并放到tomcat中启动
第一步:在idea中选中Artifacts.右侧勾选Build on make生成war包,如下图 第二步:将target文件夹里面的war包拷贝到tomcat文件下的webapp目录下 第三步:修改 ...
- Codeforces Round #538 (Div. 2) C 数论 + 求b进制后缀零
https://codeforces.com/contest/1114/problem/C 题意 给你一个数n(<=1e8),要你求出n!在b进制下的后缀零个数(b<=1e12) 题解 a ...
- Office2013 如何安装Matlab notebook
Office2013 如何安装Matlab notebook 听语音 浏览:912 | 更新:2014-09-16 07:02 1 2 3 4 5 6 7 分步阅读 Office2013(64bit) ...
- IntelliJ IDEA 2017版 spring-boot2.0.2 搭建 JPA springboot DataSource JPA环境搭建,JPA注解@ManyToOne使用详情;JPA外键设置
一.数据库原型 数据库模型如图所示,而现在需要根据数据库模型,建立对应的实体类,这在项目重构老数据库,采用新的框架重构上应该是比较常见的. 数据库脚本如下: CREATE TABLE `bomsub` ...
- cyclone iv中DDR2的本地接口时序
本地接口 信号名 方向 描述 local_burstbegin input 如果local_ready无效不起作用.IP核在local_write_req为高时,在phy_clk上升沿采样.当lo ...
- Grand Central Dispatch
什么是GCD? Grand Central Dispatch或者GCD,是一套低层API,提供了一种新的方法来进行并发程序编写.从基本功能上讲,GCD有点像NSOperationQueue,他们都允许 ...