[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 ...
随机推荐
- MyEclipse Server view报错解决方法
MyEclipse Server view报错解决方法 方法/步骤 启动MyEclipse,弹出一个框,报错. ---------------------------------------- ...
- 日志审计系统、事件日志审计、syslog审计
日志审计系统.事件日志审计.syslog审计 任何IT机构中的Windows机器每天都会生成巨量日志数据.这些日志包含可帮助您的有用信息: · 获取位于各个Windows事件日志严重性级别的所有网络活 ...
- python学习 day6 (3月7日)
#__author : 'liuyang' #date : 2019/3/7 0007 a = ['a' , 'b' , 'c'] b = [] print(a is b ) # 空元组 可以 空列表 ...
- 在RedHat 和 Ubuntu 中配置 Delphi 的Linux开发环境(转)
原文地址:http://chapmanworld.com/2016/12/29/configure-delphi-and-redhat-or-ubuntu-for-linux-development/ ...
- 使用Spring 简化MyBatis
1.导入mybatis所有的jar 和 spring 基本包,spring-jdbc,spring-tx,spring-aop,spring整合mybatis的包等. 2.编写spring配置文件ap ...
- 2018.12.19 atcoder Iroha and a Grid(组合数学)
传送门 组合数学好题. 给你一个hhh行www列的网格,其中左下角aaa行bbb列不能走,问从左上角走到右下角有多少种走法(每次只能向右或者向下) 我们考虑分步计数. 我们一共能走的区域是总网格区域去 ...
- 2018.12.12 codeforces 938E. Max History(组合数学)
传送门 唉最开始居然把题给看错了. 其实是组合数学傻逼题呢. 题意简述:给出一个数列,定义一个与数列有关的fff函数,fff函数定义如下: 首先f=0,M=1f=0,M=1f=0,M=1,一直重复如下 ...
- sqlserver 修改数据库表所有者
SQLServer修改表所有者 来自:http://www.cnblogs.com/tearer/archive/2012/09/16/2687802.html 批量修改:EXEC sp_MSfor ...
- MacOS使用常用配置
如何增加tree命令? 安装Homebrew步骤:http://blog.csdn.net/xianyiqi/article/details/51297562 安装npm步骤:https://blog ...
- 使用jstl的Foreach 和jquery的each()的index属性
最近项目中用到隔行换色问题,使用到了jstl的foreach和jquery的each进行遍历. 首先jstl技术.除了常用的items,var外,还有一个下标属性varStatus,从0开始,使用起来 ...