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];
- }
- }
- public class Solution {
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' - ...
随机推荐
- 【翻译】在GitHub上通过星级评估排名前10的最受欢迎的开源Delphi项目
GitHub上有相当多的Delphi开源项目可以为你节省一些时间.我在GitHub上搜索了Delphi,然后按最主要的项目进行排序,并列出了前十名单.这里有一些非常好的东西,包括Awesome Del ...
- xorm - Update,乐观锁,更新时间updated,NoAutoTime()
更新数据使用Update方法 Update方法的第一个参数为需要更新的内容,可以为一个结构体指针或者一个Map[string]interface{}类型. 当传入的为结构体指针时,只有非nil和非0的 ...
- 『Go基础』第4节 VS Code配置Go语言开发环境
VS Code 是微软开源的一款编辑器, 本文主要介绍如何使用VS Code搭建Go语言的开发环境. 下载与安装VS Code 官方下载地址: https://code.visualstudio.co ...
- Logrotate滚动openresty日志
一.摘要 Linux服务器上我们用Logrotate来分割归档日志文件,结合crond我们可以指定每天在某个时间自动整理日志等文档.本文主要说明了Centos下Logrotate的使用和配置的方法. ...
- SpringBoot--整合Mybatis+druid
分为两部分,首先替换默认数据源为阿里德鲁伊并添加监控,其次是SpringBoot下使用Mybatis 替换数据源为德鲁伊 首先在配置文件里配置好数据库连接的基本信息,如username passwor ...
- java之struts2之ServletAPI
在之前的学习中struts2已经可以处理大部分问题了.但是如果要将用户登录数据存入session中,可以有两种方式开存入ServletAPI. 一种解耦合方式,一种耦合方式. 1. 解耦合方式 解耦合 ...
- C# 使用代理实现方法过滤
一.为什么要进行方法过滤 一些情况下我们需要再方法调用前记录方法的调用时间和使用的参数,再调用后需要记录方法的结束时间和返回结果,当方法出现异常的时候,需要记录异常的堆栈和原因,这些都是与业务无关的代 ...
- System.Data.Entity.Core.EntityException: 可能由于暂时性失败引发了异常。如果您在连接到 SQL Azure 数据库,请考虑使用 SqlAzureExecutionStrategy。
代码异常描述 ************** 异常文本 **************System.Data.Entity.Core.EntityException: 可能由于暂时性失败引发了异常.如果 ...
- vue.js相关教程
Vue.js——60分钟快速入门 http://www.cnblogs.com/keepfool/p/5619070.html
- mySql入门-(二)
最近刚刚开始学习Mysql,然而学习MySql必经的一个过程就是SQL语句,只有按照文档从头开始学习SQL语句.学习的过程是痛苦的,但是学完的成果是甘甜的. SQL 语法 所有的 SQL 语句都以下列 ...