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' - ...
随机推荐
- Prometheus入门到放弃(4)之cadvisor监控docker容器
Prometheus监控docker容器运行状态,我们用到cadvisor服务,cadvisor我们这里也采用docker方式直接运行. 1.下载镜像 [root@prometheus-server ...
- c++快速排序原理及优化
快速排序 快速排序的时间复杂度为O(logn) 注意:快速排序主要是标志数的选取,如果所选的数恰好为最小或者最大,则是最糟糕的情况,即一轮下来数据没有发生变化! 如何选取中间的标志数成为了算法的关键. ...
- 【SQL Server数据迁移】32位的机器:SQL Server中查询ORACLE的数据
从SQL Server中查询ORACLE中的数据,可以在SQL Server中创建到ORACLE的链接服务器来实现的,但是根据32位 .64位的机器和软件,需要用不同的驱动程序来实现. 在32位的机器 ...
- Unity项目 - 简单时钟 Clock
项目展示 Github项目地址:简单时钟 Clock 制作流程 表盘绘制: 采用Aseprite 像素绘图软件绘制表盘及指针.本例钟表素材大小 256x256,存储格式为png,但发现导入Unity后 ...
- Disruptor分布式id生成策略
需要的pom文件: <!-- 顺序UUID --> <dependency> <groupId>com.fasterxml.uuid</groupId> ...
- postgres 序列
postgres序列(serial)和类型:https://www.cnblogs.com/alianbog/p/5654604.html 序列:https://www.cnblogs.com/mch ...
- 【转】SpringBoot处理url中的参数的注解
1.介绍几种如何处理url中的参数的注解 @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注解,是 @RequestMa ...
- Java中 StringBuffer StringBuilder String 区别
String 字符串常量 不可变 使用字符串拼接时是不同的2个空间 StringBuffer 字符串变量 可变 线程安全 字符串拼接直接在字符串后追加 StringBui ...
- c# 基于RTMP推流 PC+移动端 拉流播放
网上关于直播相关的文章很多,但是讲解还是不够系统,对于刚刚接触直播开发的朋友实施起来会浪费不少时间.下面结合我自己的经验, 介绍一下直播方面的实战经验. 分成两个部分 第一部分是标题中介绍的基于RTM ...
- 【开发笔记】- MD5加密
主要用于对用户密码的加密,保护用户账户安全: /** * @author shenruihai * */ import java.security.MessageDigest; import org. ...