LeetCode639. Decode Ways II——动态规划

文章目录

一、题目

A message containing letters from A-Z can be encoded into numbers using the following mapping:

‘A’ -> “1”
‘B’ -> “2”

‘Z’ -> “26”
To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, “11106” can be mapped into:

“AAJF” with the grouping (1 1 10 6)
“KJF” with the grouping (11 10 6)
Note that the grouping (1 11 06) is invalid because “06” cannot be mapped into ‘F’ since “6” is different from “06”.

In addition to the mapping above, an encoded message may contain the '’ character, which can represent any digit from ‘1’ to ‘9’ (‘0’ is excluded). For example, the encoded message "1" may represent any of the encoded messages “11”, “12”, “13”, “14”, “15”, “16”, “17”, “18”, or “19”. Decoding “1*” is equivalent to decoding any of the encoded messages it can represent.

Given a string s consisting of digits and ‘*’ characters, return the number of ways to decode it.

Since the answer may be very large, return it modulo 109 + 7.

Example 1:

Input: s = “"
Output: 9
Explanation: The encoded message can represent any of the encoded messages “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, or “9”.
Each of these can be decoded to the strings “A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, and “I” respectively.
Hence, there are a total of 9 ways to decode "
”.
Example 2:

Input: s = “1*”
Output: 18
Explanation: The encoded message can represent any of the encoded messages “11”, “12”, “13”, “14”, “15”, “16”, “17”, “18”, or “19”.
Each of these encoded messages have 2 ways to be decoded (e.g. “11” can be decoded to “AA” or “K”).
Hence, there are a total of 9 * 2 = 18 ways to decode “1*”.
Example 3:

Input: s = “2*”
Output: 15
Explanation: The encoded message can represent any of the encoded messages “21”, “22”, “23”, “24”, “25”, “26”, “27”, “28”, or “29”.
“21”, “22”, “23”, “24”, “25”, and “26” have 2 ways of being decoded, but “27”, “28”, and “29” only have 1 way.
Hence, there are a total of (6 * 2) + (3 * 1) = 12 + 3 = 15 ways to decode “2*”.

Constraints:

1 <= s.length <= 105
s[i] is a digit or ‘*’.

二、题解

class Solution {
   
public:
    int numDecodings(string s) {
   
        long long mod = 1e9+7;
        int n = s.size();
        vector<long long> dp(n + 1,0);
        dp[n] = 1;
        for(int i = n - 1;i >= 0;i--){
   
            if(s[i] != '0'){
   
                dp[i] = (s[i] == '*' ? 9 : 1) * dp[i+1];
                if(i + 1 < n){
   
                    if(s[i] != '*'){
   
                        if(s[i+1] != '*') {
   
                            if(stoi(s.substr(i,2)) <= 26) dp[i] += dp[i+2];
                        }
                        else{
   
                            if(s[i] == '1') dp[i] += dp[i+2] * 9;
                            else if(s[i] == '2') dp[i] += dp[i+2] * 6;
                        }
                    }
                    else{
   
                        if(s[i+1] != '*'){
   
                            if(s[i+1] <= '6') dp[i] += dp[i+2] * 2;
                            else dp[i] += dp[i+2];
                        }
                        else dp[i] += dp[i+2] * 15;
                    }
                }
                dp[i] = dp[i] % mod;
            }
        }
        return (int) dp[0];
    }
};

相关推荐

  1. LeetCode639. Decode Ways II——动态规划

    2024-02-09 14:22:02       42 阅读
  2. 动态规划 Leetcode 63 不同路径II

    2024-02-09 14:22:02       50 阅读
  3. LeetCode——动态规划

    2024-02-09 14:22:02       42 阅读
  4. [LeetCode]-动态规划-4

    2024-02-09 14:22:02       46 阅读
  5. leetcode动态规划专题

    2024-02-09 14:22:02       43 阅读
  6. LeetCode动态规划--题目练习

    2024-02-09 14:22:02       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-09 14:22:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-09 14:22:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-09 14:22:02       82 阅读
  4. Python语言-面向对象

    2024-02-09 14:22:02       91 阅读

热门阅读

  1. C++ .h文件类的调用

    2024-02-09 14:22:02       52 阅读
  2. 机器学习原理到Python代码实现之PolynomialRegression

    2024-02-09 14:22:02       42 阅读
  3. List 差集

    2024-02-09 14:22:02       39 阅读
  4. 侵入式智能指针和非侵入式智能指针

    2024-02-09 14:22:02       44 阅读
  5. 动态规划C语言

    2024-02-09 14:22:02       39 阅读
  6. Jetpack Room使用

    2024-02-09 14:22:02       48 阅读
  7. 开源软件:引领技术创新与商业模式转型

    2024-02-09 14:22:02       50 阅读
  8. 2024年GPT如何发展?

    2024-02-09 14:22:02       50 阅读
  9. 开源软件的未来发展趋势与应对新挑战和机遇

    2024-02-09 14:22:02       59 阅读