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. python大数据挖掘和分析的套路

    大数据的4V特点: Volume(大量):数据巨大. Velocity(高速):数据产生快,每一天每一秒全球人产生的数据足够庞大且数据处理也逐渐变快. Variety(多样):数据格式多样化,如音频数 ...

  2. Linux进程的五个段

    目录 数据段 代码段 BSS段 堆(heap) 栈 数据段 用来存放可执行文件中已初始化的全局变量,换句话说就是存放程序静态分配的变量和全局变量: 代码段 代码段是用来存放可执行文件的操作指令,也就是 ...

  3. 创建包含CRUD操作的Web API接口2:实现Get方法

    本节是前一节的延续,上一节中我们已经为我们的Web API项目创建必要的基础设施. 在本节中,我们将在我们的控制器类中实现操作方法,这些方法用来处理HTTP GET请求. 根据Web API命名约定, ...

  4. C#获取汉字拼音和首字母

    C#获取汉字拼音和首字母 引入NPinyin using NPinyin; 调用 /// <summary> /// 中文首字母大写 /// </summary> /// &l ...

  5. pycharm从本地离线添加模块

    豆瓣的源: http://pypi.douban.com/simple pip install matplotlib -i http://pypi.douban.com/simple --truste ...

  6. js 移动端之监听软键盘弹出收起

    js 移动端关于页面布局,如果底部有position:fixed的盒子,又有input,当软键盘弹出收起都会影响页面布局.这时候Android可以监听resize事件,代码如下,而ios没有相关事件. ...

  7. Vue2.0的核心思想

    Vue的核心思想为数据驱动和组件化. 一.数据驱动——双向绑定 Vue是一种MVVM框架.而DOM是数据的一个种自然映射.传统的模式是通过Ajax请求从model请求数据,然后手动的触发DOM传入数据 ...

  8. MySQL Lock--MySQL加锁学习2

    准备测试数据: ## 开启InnoDB Monitor SET GLOBAL innodb_status_output=ON; SET GLOBAL innodb_status_output_lock ...

  9. MySQL Backup--Xtrabackup备份参数

    Xtrabackup备份参数 参数选项: innobackupex [--compress] [--compress-threads=NUMBER-OF-THREADS] [--compress-ch ...

  10. 过滤器+用session验证是否登陆过

    过滤器: public class MyActionFilter : ActionFilterAttribute//继承ActionFilterAttribute类 { public override ...