码迷,mamicode.com
首页 > 其他好文 > 详细

Decode Ways II

时间:2019-12-21 22:41:47      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:map   data-   wing   show   follow   cte   ping   int   final   

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

标签:map   data-   wing   show   follow   cte   ping   int   final   

原文地址:https://www.cnblogs.com/FLAGyuri/p/12078271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!