[LintCode] 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.
Example
Given encoded message 12, it could be decoded as AB (1 2) or L (12).
The number of ways decoding 12 is 2.
LeetCode上的原题,请参见我之前的博客Decode Ways。
解法一:
class Solution {
public:
/**
* @param s a string, encoded message
* @return an integer, the number of ways decoding
*/
int numDecodings(string& s) {
if (s.empty()) return ;
int n = s.size();
vector<int> dp(n + , );
for (int i = ; i < n + ; ++i) {
if (s[i - ] == '') dp[i] = ;
else dp[i] = dp[i - ];
if (i >= && (s[i - ] == '' || (s[i - ] == '' && s[i - ] <= ''))) {
dp[i] += dp[i -];
}
}
return dp.back();
}
};
解法二:
class Solution {
public:
/**
* @param s a string, encoded message
* @return an integer, the number of ways decoding
*/
int numDecodings(string& s) {
if (s.empty()) return ;
vector<int> dp(s.size() + , );
dp[] = ;
for (int i = ; i < dp.size(); ++i) {
if (s[i - ] >= '' && s[i - ] <= '') dp[i] += dp[i - ];
if (i >= && s.substr(i - , ) <= "" && s.substr(i - , ) >= "") {
dp[i] += dp[i - ];
}
}
return dp.back();
}
};
解法三:
class Solution {
public:
/**
* @param s a string, encoded message
* @return an integer, the number of ways decoding
*/
int numDecodings(string& s) {
if (s.empty() || s.front() == '') return ;
int c1 = , c2 = ;
for (int i = ; i < s.size(); ++i) {
if (s[i] == '') c1 = ;
if (s[i - ] == '' || (s[i - ] == '' && s[i] <= '')) {
c1 = c1 + c2;
c2 = c1 - c2;
} else {
c2 = c1;
}
}
return c1;
}
};
[LintCode] Decode Ways 解码方法的更多相关文章
- 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' - ...
- [LeetCode] Decode Ways 解码方法个数、动态规划
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 091 Decode Ways 解码方法
包含 A-Z 的字母的消息通过以下规则编码:'A' -> 1'B' -> 2...'Z' -> 26给定一个包含数字的编码消息,请确定解码方法的总数.例如,给定消息为 "1 ...
- Leetcode91.Decode Ways解码方法
一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...
- [leetcode]91. Decode Ways解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] decode ways 解码方式
A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
随机推荐
- 5.linux内核模块基础,内核模块学习
linux内核模块基础 一.定义 Linux 内核的整体结构非常庞大,其包含的组件也非常多,如何使用这些组件呢: 方法 1:把所有的组件都编译进内核文件,即:zImage 或 bzImage,但这样会 ...
- C# 编码转换 UTF8转GB2312 GB2312转UTF8
/// <summary> /// GB2312转换成UTF8 /// </summary> /// <param name="text">&l ...
- 遍历PspCidTable表检测隐藏进程
一.PspCidTable概述 PspCidTable也是一个句柄表,其格式与普通的句柄表是完全一样的,但它与每个进程私有的句柄表有以下不同: 1.PspCidTable中存放的对象是系统中所有的进程 ...
- JS自定义属性兼容
var obj={}; if(obj.dataset){ obj.dataset.original="11"; }else{ obj.getAttribute("data ...
- C#复习、面向对象阶段开始
C#复习:在控制台程序中使用结构体.集合,完成下列要求项目要求:一.连续输入5个学生的信息,每个学生都有以下4个内容:1.序号 - 根据输入的顺序自动生成,不需要手动填写,如输入第一个学生的序号是1, ...
- HDU 4513 吉哥系列故事——完美队形II (Manacher变形)
题意:假设有n个人按顺序的身高分别是h[1], h[2] ... h[n],从中挑出一些人形成一个新的队形,新的队形若满足以下要求,则就是新的完美队形: 1.连续的 2.形成回文串 3.从左到中间那 ...
- 常用chrome插件推荐
下面打红色的2个强烈推荐使用: FQ的: https://chrome.google.com/webstore/detail/ecross-free/njdjpgffklilbojbobbfecfcg ...
- DSP using MATLAB 示例Example3.23
代码: % Discrete-time Signal x1(n) : Ts = 0.0002 Ts = 0.0002; n = -25:1:25; nTs = n*Ts; x1 = exp(-1000 ...
- java.net.SocketException: No buffer space available
https 访问url在调用量不大的情况下 java.net.SocketException: No buffer space available (maximum connections reach ...
- jQuery-事件以及动画
事件: 1.//方法1 $(window).load(function(){ }) window.onload=function(){ } //方法2 function one(){ alert(&q ...