[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 ...
随机推荐
- bootstrap 中 css 与 javascript 的使用
1.css 1.1.选择器 1.2.子选择器: css 里的子元素用符号'>'表示.如下示例是表示拥有table样式的表盒,其thead元素内的tr元素如果有th的话,则应用该样式. .tabl ...
- 控制台管理apk
http://www.cnblogs.com/mythou/archive/2013/06/11/3132249.html pm命令的具体用法如下: pm 命令是Android里面packageMan ...
- Windows AD域管理软件是什么?
Windows AD域管理软件是什么? ADManager Plus是一个简单易用的Windows AD域管理工具,帮助域管理员简化日常的管理工作.通过直观友好的操作界面,可以执行复杂的管理操作,比如 ...
- kbmmw 5.02发布
5.02.00 May 27 2017 Important notes (changes that may break existing code) ========================= ...
- 核心一:DI
1.DI:中文名称:依赖注入 2.英文名称:(Dependency Injection) 3.DI是什么?? 3.1 DI和IoC是一样的 3.2 当一个类(A)中需要依赖另一类(B)对象时,把B赋值 ...
- 利用xshell远程连接centos安装oracle11g时在图形界面登录
1.首先给centos安装桌面环境.( yum groupinstall ‘GNOME Desktop’) 2.安装Xmanager软件 3.打开xshell,新建连接 填好主机和名称后,点击左侧连接 ...
- 机器学习P7
优化问题: https://www.cnblogs.com/liaohuiqiang/p/7805954.html KKT条件就是把高数里面求不等式约束条件问题的分类方法写成两个条件.
- 学以致用十二-----YouCompeteMe巨坑
接上一篇,通过这几天的不断尝试,发现一个无法解决的问题.至于我安装成功的那台,我至今不知道是安装了哪一步导致成功的. 首先,我在.vimrc里加上了 Plugin 'Valloric/YouComp ...
- 如何比较两个xml 的异同
http://www.xmlunit.org/ <dependency> <groupId>org.xmlunit</groupId> <ar ...
- MATLAB二分法函数求根
function xc = bisect(f,a,b,tol) ind = b-a; while ind > tol xx = (a+b)/; b = xx; else a = xx; end ...