Decode Ways II
Description
A message containing letters from A-Z is being encoded to numbers using the following mapping way:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Beyond that, now the encoded string can also contain the character *, which can be treated as one of the numbers from 1 to 9.
Given the encoded message containing digits and the character *, return the total number of ways to decode it.
Also, since the answer may be very large, you should return the output mod 10^9 + 7.
- The length of the input string will fit in range [1, 10^5].
- The input string will only contain the character
*and digits0-9.public class Solution {
/**
* @param s: a message being encoded
* @return: an integer
*/
public int numDecodings(String s) {
if (s == null || s.length() == 0) {
return 0;
} final int mod = 1000000007;
int n = s.length();
int[] f = new int[n + 1];
f[0] = 1;
for (int i = 1; i <= n; i++) {
f[i] = 0;
if (s.charAt(i - 1) == '*') {
f[i] = (int)((f[i] + 9L * f[i - 1]) % mod);
if (i >= 2) {
if (s.charAt(i - 2) == '*') {
f[i] = (int)((f[i] + 15L * f[i - 2]) % mod);
}
else if (s.charAt(i - 2) == '1') {
f[i] = (int)((f[i] + 9L * f[i - 2]) % mod);
}
else if (s.charAt(i - 2) == '2') {
f[i] = (int)((f[i] + 6L * f[i - 2]) % mod);
}
}
}
else {
if (s.charAt(i - 1) != '0') {
f[i] = (f[i] + f[i - 1]) % mod;
}
if (i >= 2) {
if (s.charAt(i - 2) == '*'){
if (s.charAt(i - 1) <= '6') {
f[i] = (int)((f[i] + 2L * f[i - 2]) % mod);
}
else {
f[i] = (f[i] + f[i - 2]) % mod;
}
}
else {
int twoDigits = (s.charAt(i - 2) - '0') * 10 + s.charAt(i - 1) - '0';
if (twoDigits >= 10 && twoDigits <= 26) {
f[i] = (f[i] + f[i - 2]) % mod;
}
}
}
}
} return f[n];
}
}
Decode Ways II的更多相关文章
- [LeetCode] Decode Ways II 解码方法之二
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- leetcode 639 Decode Ways II
首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1] ...
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- [Swift]LeetCode639. 解码方法 2 | Decode Ways II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- 639. Decode Ways II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 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 ...
- 91. Decode Ways反编译字符串
[抄题]: A message containing letters from A-Z is being encoded to numbers using the following mapping: ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
随机推荐
- 对于并发任务,应该使用 Task 替代 BackgroundWorker
背景 EF + Oracle,并发存储监控记录,使用 BackgroundWorker 时产生错误如下: public void MonitorLogging(DateTime DateStart, ...
- Spring Boot取消默认tomcat启动,打成war包发布到服务器运行
一.设置打包方式 在pom.xml中设置打包格式 <packaging>war</packaging> 二.取消Spring Boot的tomcat <!--部署成war ...
- isset和empty,isset和unset,__isset和__unset
1.isset() 用来检测变量是否存在,如果变量存在,且值不等于零,返回ture empty() 用来检测是否为空,如果变量值值为"".0."0".NULL. ...
- python模块知识三 hashlib 加密模块、collections、re模块
8.hashlib 加密模块 主要用于加密和校验 常见密文:md5,sha1,sha256,sha512 只要明文相同,密文就相同 只要明文不相同,密文就是不相同的 不能反逆(不能解密)--md5 ...
- TensorFlow学习笔记(1)—— 基本概念与框架
入门框架时的常见问题 学习框架的原因? 方便.易用 学习框架的哪些知识点? 掌握一个项目的基本流程,就知道需要学习哪些知识点了 迅速学习框架的方法 根据项目每块流程的需要针对性的学 可以看官方的入门教 ...
- char * const * (*a) (int b)
char * const * (*a) (int b), 按照c++ program language的读法,从右往左读,* 读作pointer to 把(*a) (int b看作整体, (*a) ( ...
- 中国大学MOOC-翁恺-C语言程序设计习题集(二)
04-0. 求符合给定条件的整数集(15)给定不超过6的正整数A,考虑从A开始的连续4个数字.请输出所有由它们组成的无重复数字的3位数. 输入格式: 输入在一行中给出A. 输出格式: 输出满足条件的的 ...
- bootstrap table 列表增加输入框并保存输入的值不清除
需求: 在bootstrap table上增加输入框,需要选择的时候把输入的值保存到 row 里面,传递给其他模块使用. 实现: columns: [{ ...., { field: 'myField ...
- oracle数据库 部分函数的用法
select * from tab; //获取当前用户的数据库的所有表名 select sys_guid(),UserName from TESTLIKUI; //获取guid select sys_ ...
- C# vb .net实现扭曲角特效滤镜图像处理
在.net中,如何简单快捷地实现Photoshop滤镜组中的扭曲角效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一 ...