leetcode 639 Decode Ways II

首先回顾一下decode ways I 的做法:链接

分情况讨论

if s[i]==‘*‘

  考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1

  再考虑s[i-1]和s[i]decode,如果s[i-1]为*,那么**的组合共有15种(11,12,13.....,21,22..26),注意不是9*9=81种,因为我们是s[i-1]和s[i]一起decode,如果是38这样是大于26的

              如果s[i-1]不为*,枚举s[i],组合起来小于26即可

if s[i]!=‘*‘

  考虑s[i]单独decode,  和decode ways I 一样,考虑s[i]为0的情况

  再考虑s[i-1]和s[i] decode 如果s[i-1]为*, 那么 *和s[i]组合要小于26,直接枚举判断就行了

              如果s[i-1]不为*,枚举s[i-1],组合起来小于26即可

不用条件运算符会更快,但用了代码会简洁很多

 1 class Solution {
 2 public:
 3     int numDecodings(string s) {
 4         int n=s.length();
 5         if(n==0)    return 0;
 6         vector<int> dp(n+1,0);
 7         long long dp1=0,dp2=0,now;
 8         if(s[0]!=‘0‘)   dp1=s[0]==‘*‘?9:1;
 9         for(int i=1;i<n;i++){
10             if(s[i]==‘*‘){
11                 now=dp1*9;
12                 if(s[i-1]==‘*‘) now+=i-2<0?15:dp2*15;
13                 else{
14                     for(int j=1;j<10;j++){
15                         if(s[i-1]!=‘0‘&&((s[i-1]-‘0‘)*10+j<=26))    now+=i-2<0?1:dp2;
16                     }
17                 }
18             }
19             else{
20                 now=s[i]==‘0‘?0:dp1;
21                 if(s[i-1]==‘*‘){
22                     for(int j=1;j<10;j++){
23                         if(j*10+s[i]-‘0‘<=26)   now+=i-2<0?1:dp2;
24                     }
25                 }
26                 else{
27                     if(s[i-1]!=‘0‘&&((s[i-1]-‘0‘)*10+s[i]-‘0‘<=26)) now+=i-2<0?1:dp2;
28                 }
29             }
30             dp2=dp1%(1000000007);
31             dp1=now%(1000000007);
32         }
33         return (int)dp1;
34     }
35 };
时间: 2024-10-06 10:50:28

leetcode 639 Decode Ways II的相关文章

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 动态规划 Decode Ways

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Decode Ways Total Accepted: 8689 Total Submissions: 55465 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a

[LeetCode OJ] 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

题目链接:https://leetcode.com/problems/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 num

[LeetCode] 91. Decode Ways 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 enco

leetcode No91. Decode Ways

Question: 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, Giv

【leetcode】Decode Ways(medium)

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