题目来源: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
A message containing letters fromclass 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-Z
is being encoded to numbers using the following mapping way:'A' -> 1 'B' -> 2 ... 'Z' -> 26Beyond 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 = 18Note:
- The length of the input string will fit in range [1, 105].
- The input string will only contain the character '*' and digits '0' - '9'.