91. Decode Ways

194 篇文章 0 订阅
32 篇文章 0 订阅

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".

Given a string s containing only digits, return the number of ways to decode it.

The answer is guaranteed to fit in a 32-bit integer.

 

Example 1:

Input: s = "12"
Output: 2
Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).

Example 2:

Input: s = "226"
Output: 3
Explanation: "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).

Example 3:

Input: s = "0"
Output: 0
Explanation: There is no character that is mapped to a number starting with 0.
The only valid mappings with 0 are 'J' -> "10" and 'T' -> "20", neither of which start with 0.
Hence, there are no valid ways to decode this since all digits need to be mapped.

Example 4:

Input: s = "06"
Output: 0
Explanation: "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06").

Constraints:

  • 1 <= s.length <= 100
  • s contains only digits and may contain leading zero(s).

题目链接:(4) Decode Ways - LeetCode

class Solution {
public:
    int numDecodings(string s) {
      if(s[0]=='0')
      {
          return 0;
      }
      int len=s.size(),dp[len+1];
      dp[0]=1;
      dp[1]=(s[0]=='1' || s[0]=='2' && s[1]<'7')+ (s[1]!='0');
        for(int i=2;i<len;++i)
        {
            if(s[i]=='0'&&(s[i-1]=='0'||s[i-1]>'2'))//出现了00 或者 30,40,这种就无解了
            {
                return 0;
            }
            dp[i]=s[i] != '0' ? dp[i - 1] : 0;//正常情况,s[i]在1-9范围内,dp[i]=dp[i-1],表示可以单独划分
            //,dp[i]就可以等于dp[i-1]了,不然就只能依靠和s[i-1]共同组成一种划分方式了,方法数就是dp[i-2]
            if(s[i-1]=='1'||s[i-1]=='2'&&s[i]<'7')
            {
                dp[i]+=dp[i-2]; //这个 || 可以实现 10-26,比如19,第一个条件成立,整个条件就成立了
            }
        }
        return dp[len-1];
    }
};

这题的加强版(4) Decode Ways II - LeetCode有点难,粘了一份代码放在这里

class Solution {
public:
    int numDecodings(string s) {
      int mod=1000000007;
        vector<long> count(s.length()+1,0);
        if(s.length()==0||s[0]=='0')
        {
            return 0;
        }
        count[0]=1;
        count[1]=1;
        if(s[0]=='*')
        {
            count[1]=9;
        }
       
        for(int i=2;i<=s.length();i++)
        {
            if(s[i-1]>'0'&&s[i-1]<='9')
            {
                count[i]=count[i-1];
            }
            if(s[i-1]=='*')
            {
                count[i]=(count[i-1]*9)%mod;
                if(s[i-2]=='1')
                {
                    count[i]+=(count[i-2]*9)%mod;
                    count[i]%=mod;
                }
                if(s[i-2]=='2')
                {
                    count[i]+=(count[i-2]*6)%mod;
                    count[i]%=mod;
                }
                if(s[i-2]=='*')
                {
                    count[i]+=(count[i-2]*15)%mod;
                    count[i]%=mod;
                }
            }
            else if(s[i-2]=='1'||s[i-2]=='2'&&s[i-1]>='0'&&s[i-1]<='6')
            {
                count[i]+=count[i-2];
                count[i]%=mod;
            }
            else if(s[i-2]=='*')
            {
                if(s[i-1]<='6')
                {
                    count[i]+=(count[i-2]*2)%mod;
                    count[i]%=mod;
                }
                else
                {
                    count[i]+=count[i-2];
                    count[i]%=mod;
                }
            }
            
        }
        
        return count[s.length()];
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值