Leetcode 91. Decode Ways&&639.Decode ways

题目来源:leetcode

Decode IWays I
A message containing letters fromA-Zis 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 message"12", it could be decoded as"AB"(1 2) or"L"(12).

The number of ways decoding"12"is 2.

思路:

字符串S每增加一位,当他自己作为以为解码的情况 ,相当于他的前n-1为的个数,同时他也可能跟前以为组成两位数此时就是前n-2的个数,两个想家
 * 考虑与前一位不能构成1-26之间,只能是他自己单独解码
 * 考虑改为是0的特殊情况
/**字符串S每增加一位,当他自己作为以为解码的情况 ,相当于他的前n-1为的个数,同时他也可能跟前以为组成两位数此时就是前n-2的个数,两个想家
 * 考虑与前一位不能构成1-26之间,只能是他自己单独解码
 * 考虑改为是0的特殊情况
 * Created by a819 on 2017/8/21.
 */
public class NumDecoding {
    public int numDecodings(String s) {
        if (s.length()==0)
            return 0;
        int dp[]=new int[s.length()+1];
        dp[0]=1;
        if (s.charAt(0)=='0')
            dp[1]=0;
        else dp[1]=1;
        for (int i=2;i<=s.length();i++) {
            if (s.charAt(i - 1) == '0') {
                if (Integer.parseInt(s.substring(i - 2, i)) <= 26 && Integer.parseInt(s.substring(i - 2, i)) >= 10)
                    dp[i] = dp[i - 2] ;
                else return 0;
            } else {
                if (Integer.parseInt(s.substring(i - 2, i)) <= 26&&Integer.parseInt(s.substring(i - 2, i))>=10)
                    dp[i]=dp[i-1]+dp[i-2];
                else dp[i]=dp[i-1];
            }
        }
        return dp[s.length()];

    }
}
II
 
 
class Solution {
    public int numDecodings(String s) {
             if (s.length()==0)//字符串为空 返回0
            return 0;
        int dp[]=new int[s.length()+1];//记录解码个数
        dp[0]=1;
        if (s.charAt(0)=='0')
            dp[1]=0;
        else if (s.charAt(0)=='*')
            dp[1]=9;
        else dp[1]=1;
        for (int i=2;i<=s.length();i++){
            if (s.charAt(i-1)=='*'){//该为是否是*
                if (s.charAt(i-2)=='*'){//**
                    dp[i]=dp[i-1]*9+dp[i-2]*(26-11);

                }
                else if (s.charAt(i-2)=='1')//1*
                    dp[i]=dp[i-1]*9+dp[i-2]*9;
                else if(s.charAt(i-2)=='2')//2*
                    dp[i]=dp[i-1]*9+dp[i-2]*6;//我觉得应该是7  傻逼了人家说了*不能是0
                else dp[i]=dp[i-1]*9;
            }
            else if (s.charAt(i - 1) == '0') {//是否为0
                if (s.charAt(i-2)!='*') {//
                    if (Integer.parseInt(s.substring(i - 2, i)) <= 26 && Integer.parseInt(s.substring(i - 2, i)) >= 10)
                        dp[i] = dp[i - 2];
                    else return 0;
                }
                else dp[i]=dp[i-2]*2;//*0   10,20
            } else {
                if(s.charAt(i-2)!='*'){
                if (Integer.parseInt(s.substring(i - 2, i)) <= 26&&Integer.parseInt(s.substring(i - 2, i))>=10)
                    dp[i]=dp[i-1]+dp[i-2];
                else dp[i]=dp[i-1];
                }
                else {if(s.charAt(i-1)>='0'&&s.charAt(i-1)<='6')
                    dp[i]=dp[i-1]+dp[i-2]*2;
                     else dp[i]=dp[i-2]+dp[i-1];
                     }
            }
        }
    return dp[s.length()];
    }
}
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 109 + 7.

Example 1:

Input: "*"
Output: 9
Explanation: The encoded message can be decoded to the string: "A", "B", "C", "D", "E", "F", "G", "H", "I".

Example 2:

Input: "1*"
Output: 9 + 9 = 18

Note:

  1. The length of the input string will fit in range [1, 105].
  2. The input string will only contain the character '*' and digits '0' - '9'.


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值