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' - ...
随机推荐
- linux根据进程名终止进程
2017年09月25日 19:44:32 aladdin_sun 阅读数 5235 linux根据进程名终止进程 实验环境 操作系统:CentOS Linux release 7.3.1611 ( ...
- day37——阻塞、非阻塞、同步、异步
day37 阻塞.非阻塞.同步.异步 进程运行的三个状态:运行.就绪.阻塞 执行的角度 阻塞:程序运行时,遇到了IO,程序挂起,CPU被切走 非阻塞:程序没有遇到IO,程序遇到IO但是我通过某种手段, ...
- linux shell程序常用功能
一.循环读取文件 循环读取文件方式有多种,推荐下列方法 while read line;do local include=$(echo ${line} | grep "filter" ...
- Python 获取本月的最后一天
一.需求 现在有一个场景,需要每月的最后一天,发送一封邮件. 二.获取本月最后一天 有没有办法使用Python的标准库轻松确定(即一个函数调用)给定月份的最后一天? 答案是有的,使用 datetime ...
- Angular2 环境的搭建
在慕课网上学习angular,自己也动手做一做. 1:Nodejs的安装: https://nodejs.org/en/ 下载--安装. (这一步中是将npm也安装了,记住自己选择的目录,我这里 ...
- iOS-右滑返回,利用Runtime添加全屏Pop手势
项目中经常会遇到类似需求,需要在某控制器增加全屏右滑返回功能. 在我们不隐藏 NavigationBar 的前提下,系统会自动替我增加此功能,只是它作用的范围仅仅在屏幕左边有限区域. 我们需要在整个界 ...
- 二叉排序树详解——PHP代码实现
二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),亦称二叉搜索树. 一.定义 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: 若左子树不空 ...
- 1+X证书学习日志 —— css样式表
## 因为初级的内容较多,所以选了一些有用的 需要记忆的内容写下 方便日后回顾 CSS语法 选择符{属性:属性值;} ## 所有的css代码 都要放在css样式表里面 ...
- 超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用)
超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用) 转载自:https://www.jianshu.com/p/2ad8c8b5bf75 亲测有效~ <tem ...
- DML 操作表中数据
DML 是对于表中的记录进行增删改操作 一.添加数据 语法格式: insert into 表名[字段名] values[字段值] 表名:表示往那张表中添加数据 (字段名1,字段名2, ...