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. spring框架学习(三)——AOP( 面向切面编程)

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

  2. voltile解析

    https://www.cnblogs.com/dolphin0520/p/3920373.html

  3. ColorMatrixFilter色彩矩阵滤镜

    ColorMatrixFilter色彩矩阵滤镜: /** * * *----------------------------------------* * | *** ColorMatrixFilte ...

  4. [LOJ6433] [PKUSC2018] 最大前缀和

    题目链接 LOJ:https://loj.ac/problem/6433 Solution 注意到最大前缀要满足什么性质,假设序列\(a[1..n]\)的最大前缀是\(s_x\),那么显然要满足所有\ ...

  5. LOJ2026 JLOI/SHOI2016 成绩比较 组合、容斥

    传送门 感觉自己越来越愚钝了qwq 先考虑从\(n-1\)个人里安排恰好\(k\)个人被碾压,然后再考虑如何分配分数,两者乘起来得到答案. 对于第一部分,可以考虑容斥:设\(f_i\)表示\(i\)个 ...

  6. Java 平衡二叉树和AVL

      与BST<> 进行对比 import java.util.ArrayList; import java.util.Collections; public class Main { pu ...

  7. 使用 SetParent 跨进程设置父子窗口时的一些问题(小心卡死)

    原文:使用 SetParent 跨进程设置父子窗口时的一些问题(小心卡死) 在微软的官方文档中,说 SetParent 可以在进程内设置,也可以跨进程设置.当使用跨进程设置窗口的父子关系时,你需要注意 ...

  8. 实战远程文件同步(Remote File Sync)

    1. 远程文件同步的常见方式: 1.cron + rsync 优点: 简单 缺点:定时执行,实时性比较差:另外,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输.如果文件数量达到了百万甚 ...

  9. 11.15java实习生面试总结

    坐了两个小时的车,到了面试地点面了十五分钟左右就结束了,心里有一点难受,不过这也是刚开始,后面的路还长着呢,所以先把面试官问的题目记录下来. 1.C语言能否跨平台? 虽然我面的是java实习生,但是因 ...

  10. Lua for - ipairs,pairs,# 遍历的运行时间