Decode Ways [leetcode] DP

题目:求将数字字符串s转换成字母有多少种可能的结果?

dp(i):s[0...i-1]可能的结果数。我们设si = i-1

分析s[si]:

1. s[si - 1] = ‘1‘ || s[si - 1] = ‘2‘ && s[si] = ‘6‘

s[0...si]可以转换成s[si]对应的字母 + s[0...si-1]对应的字母,也可能是 s[si-1..si]对应的字母 + s[0...si-2]对应的字母

即dp(i) = dp(i -1) + dp(i - 2)

2. s[si] = ‘0‘ 这个情况比较容易被忽略

2.1 s[si - 1] = ‘1‘ || s[si - 1] = ‘2‘

s[0...si]可以转换成s[si-1...si]对应字母 + s[0...si-2]对应的字母

dp(i) = dp(i - 2)

2.2 s[si - 1]不存在或者为其他数字

dp(i) = 0;

3.其他情况

dp(i) = dp(i-1)

代码如下

    int numDecodings(string s) {
        vector<int> dp(s.size() + 1);
        dp[0] = 1;
        int i = 1;
        for (; i <= s.size(); i++)
        {
            int si = i - 1;
            if (s[si] == '0')
            {
                if (si - 1 >= 0 && (s[si - 1] == '1' || s[si - 1] == '2'))
                    dp[i] = dp[i - 2];
                else
                    dp[i] = 0;
            }
            else if (si - 1 >= 0 && s[si - 1] == '1' || s[si - 1] == '2' && s[si] <= '6')
                dp[i] = dp[i - 1] + dp[i - 2];
            else
                dp[i] = dp[i - 1];
        }
        return s.size() == 0 ? 0 : dp[s.size()];
    }
时间: 2024-10-12 22:36:10

Decode Ways [leetcode] DP的相关文章

[LeetCode] Decode Ways(DP)

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded

Decode Ways leetcode java

题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given enc

Decode Ways -- leetcode

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded

[LeetCode] Decode Ways [33]

题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given enco

LeetCode: Decode Ways [090]

[题目] A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given en

【leetcode刷题笔记】Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example,Given encoded

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给一串包含数字的加密报文,求有多少种解码方式 举个例子,已知报文"12",它可以解码为AB(1 2),也可以是L (12) 所以解码方式有2种. 测试样例 Input: "0" "121212" "1010

[LeetCode]91.Decode Ways

题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 - 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encode

LeetCode之Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded