leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/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:
bool check(char a, char b) {
int na = a - '0', nb = b - '0';
if(na == 0) return false;
if(na*10 + nb >= 1 && na*10 + nb <= 26) return true;
return false;
}
int numDecodings(string s) {
if(s.length() == 0) return 0;
vector<int> dp(s.length(), 0);
dp[0] = (s[0]=='0')? 0: 1;
if(s.length() == 1) return dp[0];
if(dp[0] == 0) return 0;
else {
bool flag = check(s[0], s[1]);
if(s[1] == '0' && flag) dp[1] = 1;
else if(s[1] == '0' && !flag) return 0;
else if(s[1] != '0' && flag) dp[1] = 2;
else if(s[1] != '0' && !flag) dp[1] = 1;
}
for(int i=2;i<s.length();++i) {
if(s[i] == '0') {
if(check(s[i-1], s[i])) dp[i] = dp[i-2];
else return 0;
}
else {
if(check(s[i-1], s[i])) dp[i] = dp[i-1] + dp[i-2];
else dp[i] = dp[i-1];
}
}
return dp[s.length()-1];
}
};
leetcode 91: Decode Ways
leetcode@ [91] Decode Ways (Dynamic Programming)的更多相关文章
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- leetcode 91 Decode Ways I
令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- leetcode 91 Decode Ways ----- java
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' - ...
- Leetcode#91 Decode Ways
原题地址 动态规划题,注意0导致的小陷阱. 代码: int numDecodings(string s) { ] < ] > ; ] >= ] <= : ; ; int nex ...
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 91. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
随机推荐
- WPF 视图分组排序
视图分组排序 效果: 实现步骤: 第一步:为分组做一个标题头,就是效果图中的浅蓝色部分: <DataGrid.GroupStyle>标签部分: <DataGrid x:Name=&q ...
- POJ3690+位运算
题意:给定一个01矩阵.T个询问,每次询问大矩阵中是否存在这个特定的小矩阵. /* 64位的位运算!!! 题意: 给定一个01矩阵.T个询问,每次询问大矩阵中是否存在这个特定的小矩阵. (64位记录状 ...
- 【BZOJ 1038】 1038: [ZJOI2008]瞭望塔
1038: [ZJOI2008]瞭望塔 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 ...
- redis的key过期时间
public void set(String key,String value,int liveTime){ this.set(key, value); this.getJedis().expire( ...
- SDIBT2666——逆波兰表达式求值
逆波兰表达式求值(栈和队列) Description 从键盘上输入一个逆波兰表达式,用伪码写出其求值程序.规定:逆波兰表达式的长度不超过一行,以@符作为输入结束,操作数之间用空格分隔,操作符只可能有+ ...
- Civil3D二次开发 启动Civil3D异常
用Com方式启动Civil3D时,经常会在第一次启动时出现各种异常. 1. RPC_E_CALL_REJECTED 0x80010001 被呼叫方拒绝接收呼叫 解决方案:外部程序通过COM启动Auto ...
- C++11内存模型的粗略解释
基本解释 C++11引入了多线程,同时也引入了一套内存模型.从而提供了比较完善的一套多线程体系.在单线程时代,一切都很简单.没有共享数据,没有乱序执行,所有的指令的执行都是按照预定的时间线.但是也正是 ...
- JQUERY 选择器 总结,比较全
jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个 ...
- bzoj3205
和bzoj2595类似,也是斯坦纳树 设f[l,r,]表示在点i机器人组合成了l-r最少推的次数,然后可得 f[l,r,i]=min(f[l,m,i]+f[m+1,r,i]) f[l,r,i]=min ...
- [LOJ 1248] Dice (III)
G - Dice (III) Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Descri ...