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.

  1. The length of the input string will fit in range [1, 10^5].
  2. The input string will only contain the character * and digits 0 - 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的更多相关文章

  1. [LeetCode] Decode Ways II 解码方法之二

    A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...

  2. leetcode 639 Decode Ways II

    首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1] ...

  3. [LeetCode] 639. Decode Ways II 解码方法 II

    A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...

  4. [Swift]LeetCode639. 解码方法 2 | Decode Ways II

    A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...

  5. 639. Decode Ways II

    A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...

  6. [LeetCode] Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  7. 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 ...

  8. 91. Decode Ways反编译字符串

    [抄题]: A message containing letters from A-Z is being encoded to numbers using the following mapping: ...

  9. [LeetCode] 91. Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

随机推荐

  1. Django重新添加字段然后迁移给定默认值依然迁移不生效

    1.将对应app下的migrations文件夹下面的除了__init__.py文件外全部删除 2.delete from django_migrations where app='当前模型的app名称 ...

  2. PHP生成随机单词

    class GenRandWords { private static $_alphas = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ' ...

  3. Golang 实现单例模式

    目录 只适用于单线程环境 支持并发版本 优化并发版本 sync.Once版本 只适用于单线程环境 package main import "fmt" type Single str ...

  4. xxx商城之商品管理

  5. 第一个.NET小程序

    一.用户需求 做一个简单的网页版销售合同签核系统 1.业务员需要在手机或者电脑上操作,Key入销售合同 2.业务员填入相应的合同信息,对应主管签核 3.最终签核完,生成PDF版的销售合同,且上面自动加 ...

  6. “http”和“https”的区别是什么?优缺点是什么?

    1. http 的URL 以http:// 开头,https以https:// 开头. 2. http 标准端口是80 ,https是443. 3.https 协议需要到ca申请证书,http不需要. ...

  7. Angular应用架构设计-3:Ngrx Store

    这是有关Angular应用架构设计系列文章中的一篇,在这个系列当中,我会结合这近两年中对Angular.Ionic.甚至Vuejs等框架的使用经验,总结在应用设计和开发过程中遇到的问题.和总结的经验, ...

  8. Centos6.10编译安装php-7.1.12并安装redis模块

    1.服务器初始化 yum update -yyum install epel-release -yyum install gcc gcc-c++ wget lsof lrzsz telnet -y 2 ...

  9. Ldr和bl指令

    Ldr和bl在启动程序中,都是可以负责pc跳转的指令. 1)bl是地址无关指令,和什么地址无关呢?和当前的运行地址无关,链接器脚本中标明了一个运行地址,但是arm中的代码实际是从地址0开始运行的.这个 ...

  10. RabbitMQ direct交换器

    1:队列和交换器多次绑定 String[] routekeys={"routekey1","routekey2","routekey3"}; ...