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' - ...
随机推荐
- 链表习题(2)-一个集合用带头结点的单链表L表示,编写算法删除其值最大的结点。
/*一个集合用带头结点的单链表L表示,编写算法删除其值最大的结点.*/ /* 算法思想:使用pre,p,premax,max四个指针,pre和p进行比较,premax和max进行最后的删除操作 通过遍 ...
- SQL——ORDER BY关键字
一.ORDER BY关键字用法 ORDER BY关键字用于对数据进行排序,默认ASC(升序),可以DESC关键字变为降序. ORDER BY关键字语法: SELECT * from 表名 WHERE ...
- TZOJ5255: C++实验:三角形面积
#include<iostream> #include<iomanip> #include<math.h> #include<cmath> using ...
- MySQL数据库的安装(Windows平台)
1.MySQL数据库安装与配置 1.1 数据库安装和配置 安装需要注意的地方: 典型安装:安装最常用的特性组件,会默认安装至C盘目录下,适合大部分开发者. 自定义安装:可以自定义安装目录,自定义选择安 ...
- hadoop 异常
2019-09-20 22:49:51,955 WARN org.apache.hadoop.hdfs.server.datanode.DataNode: Problem connecting to ...
- JNA的应用
一.了解JNA之前,我们先了解一下JNA的前身JNI(Java Native Interface):通过使用 Java本地接口书写程序,可以确保代码在不同的平台上方便移植. [1] 从Java1.1 ...
- Vue使用QRCode.js生成二维码
1.安装qrcode npm install qrcode 2.组件中引入qrcode import QRCode from 'qrcode' 3.html代码 <div><span ...
- Android 低功耗蓝牙BLE 开发注意事项
基本概念和问题 1.蓝牙设计范式? 当手机通过扫描低功耗蓝牙设备并连接上后,手机与蓝牙设备构成了客户端-服务端架构.手机通过连接蓝牙设备,可以读取蓝牙设备上的信息.手机就是客户端,蓝牙设备是服务端. ...
- gin 页面重定向
两种gin页面重定向方式 redirect: package main import ( "github.com/gin-gonic/gin" "net/http&quo ...
- Redis 学习-安装、数据类型与 API 理解、Java 客户端
本博客是在学习<Redis从入门到高可用,分布式实践>教程时的笔记. 同时参考: https://www.cnblogs.com/jiang910/p/10020048.html 一.Re ...