Leetcode 91. Decode Ways
2016-01-31 09:15
267 查看
A message containing letters from
mapping:
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message
2) or
The number of ways decoding
Analysis: this is also a dynamic programming problem: we just need to decide how many conditions there to get the numbres.
1 if the char is '0',
2 if the char is from '1' to '6'
3 if the char is from '7' to '9'
and then based on the conditions above to decide how to decode the string.
public class Solution {
public int numDecodings(String s) {
int length = s.length();
if(length <= 0)
return 0;
int[] dp = new int[length + 1];
dp[0] = 1;
dp[1] = 1;
//this is a corner case.
char tem = s.charAt(0);
if(tem == '0')
return 0;
for(int i = 2; i <= length; i++){
char temChar = s.charAt(i - 1);
if(temChar == '0')
{
char pre = s.charAt(i - 2);
if(pre - '1' == 0 || pre - '1' == 1)
dp[i] = dp[i - 2];
else
return 0;
}
else if(temChar - '1' >= 0 && temChar - '6' <= 0 )
{
char pre = s.charAt(i - 2);
if(pre - '1' == 0 || pre - '1' == 1)
dp[i] = dp[i - 2] + dp[i - 1];
else
dp[i] = dp[i - 1];
}
else{
char pre = s.charAt(i - 2);
if(pre - '1' == 0)
dp[i] = dp[i - 2] + dp[i - 1];
else
dp[i] = dp[i - 1];
}
}
return dp[length];
}
}
A-Zis 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.
Analysis: this is also a dynamic programming problem: we just need to decide how many conditions there to get the numbres.
1 if the char is '0',
2 if the char is from '1' to '6'
3 if the char is from '7' to '9'
and then based on the conditions above to decide how to decode the string.
public class Solution {
public int numDecodings(String s) {
int length = s.length();
if(length <= 0)
return 0;
int[] dp = new int[length + 1];
dp[0] = 1;
dp[1] = 1;
//this is a corner case.
char tem = s.charAt(0);
if(tem == '0')
return 0;
for(int i = 2; i <= length; i++){
char temChar = s.charAt(i - 1);
if(temChar == '0')
{
char pre = s.charAt(i - 2);
if(pre - '1' == 0 || pre - '1' == 1)
dp[i] = dp[i - 2];
else
return 0;
}
else if(temChar - '1' >= 0 && temChar - '6' <= 0 )
{
char pre = s.charAt(i - 2);
if(pre - '1' == 0 || pre - '1' == 1)
dp[i] = dp[i - 2] + dp[i - 1];
else
dp[i] = dp[i - 1];
}
else{
char pre = s.charAt(i - 2);
if(pre - '1' == 0)
dp[i] = dp[i - 2] + dp[i - 1];
else
dp[i] = dp[i - 1];
}
}
return dp[length];
}
}
相关文章推荐
- github学习(1)
- 【高斯消元】[SGU275]To xor or not to xor
- addslashes()、stripslashes()和magic_quotes_gpc
- Excel自己定义纸张打印设置碰到无法对上尺寸的问题
- KM算法
- 自定义旋转的ProgressBar
- Cocos2d-x 3.1.1 Lua实例-AccelerometerTest(重力加速计)
- 苹果 企业 开发者账号 注册、申请续费整个流程($299的账号)
- android项目中刷新activity界面
- UESTC-888-Absurdistan Roads(kruskal+floyd)
- [LeetCode] Binary Tree Upside Down 二叉树的上下颠倒
- Java设计模式编程中的工厂方法模式和抽象工厂模式
- 数学符号及读法大全
- HDOJ 5616-Jam's balance【模拟】
- Eclipse下通过Maven的Jetty插件运行Web工程的配置
- 把字符串转换为整数
- 通过条件的查询----涉及到三层(Action Service DAO)
- 2015杂谈
- poj 1704 Georgia and Bob(阶梯博弈)
- 奔小康赚大钱 hdu 2255