LeetCode题解 week16
来源:互联网 发布:three.js 视角拉近 编辑:程序博客网 时间:2024/06/05 14:06
639. Decode Ways II
A message containing letters from 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’.
这道题与79.Decode Ways都是求一串数字有多少种译码方式,他们的区别就在于Decode Ways Ⅱ多了一个符号:*号。*号可以为1~9(不包括0!)的任意一个数。*号的加入,使得整个解码的方式更加多样性。
我们分情况来讨论。输入的数字串为s,result[i+1]为s中以第i个数结尾的字符串有多少种译码方式。
1.s不可能以0开头,直接返回0;
2.对于单个数字进行译码,也就是对于s[0~i],将s[0~i-1]和s[i]分别译码,若s[i]为1~9,s[i]只有1种译码结果;若s[i]为*,s[i]有9种译码结果。若i为0,不能单独译码,不予考虑。(译码的结果可能数对应k的值)
3.对于两个数字进行译码,也就是对于s[0~i],将s[0~i-2]和s[i-1~i]分别译码,若s[i-1~i]为“**”,那么一共有15种译码方式(A~Z一共有26个字母,而由于*不为0,所以除去1~10和20的情况,剩下的一共有15个字母,也就是**能够表达的字母数量)。对于1*,可以有11~19,一共9种译码方式。对于2*,可以有21~26,一共有6种译码方式。对于*[0~6],就有1[0~6],2[0~6],一共2种译码方式。对于*[7~9],就只有1[7~9]一种译码方式。其他情况都无法针对前后两个数字进行译码。(译码的结果可能数对应l的值)
而k和l的值都由上面的情况计算得出。而result[s.length()]便是我们要求的值。
另外,由于数值可能会非常大,int的范围可能不够大,会发生溢出情况,所以可能需要使用long long长整型来进行储存这些中间值。
参考代码如下:
class Solution {public: int numDecodings(string s) { int len = s.length(); if(len == 0 || s[0] == '0') return 0; if(len == 1 && s[0] != '*') return 1; if(len == 1 && s[0] == '*') return 9; long long result[len+1]; result[0] = 1; result[1] = 1; if(s[0] == '*') result[1] = 9; int max = pow(10, 9) + 7; for(int i = 1; i < len; i++) { if(s[i-1] == '0') { if(s[i] == '0') return 0; else if(s[i] == '*') result[i+1] = result[i] * 9; else result[i+1] = result[i]; } else if(s[i-1] == '*') { if(s[i] == '0') result[i+1] = result[i-1] * 2; else if(s[i] == '*') result[i+1] = result[i] * 9 + result[i-1] * 15; else if('1' <= s[i] && s[i] <= '6' ) result[i+1] = result[i] + result[i-1] * 2; else result[i+1] = result[i] + result[i-1]; } else if(s[i-1] == '1') { if(s[i] == '0') result[i+1] = result[i-1]; else if(s[i] == '*') result[i+1] = result[i] * 9 + result[i-1] * 9; else result[i+1] = result[i] + result[i-1]; } else if(s[i-1] == '2') { if(s[i] == '0') result[i+1] = result[i-1]; else if(s[i] == '*') result[i+1] = result[i] * 9 + result[i-1] * 6; else if(canCombine(s[i-1], s[i])) result[i+1] = result[i] + result[i-1]; else result[i+1] = result[i]; } else { if(s[i] == '0') return 0; else if(s[i] == '*') result[i+1] = result[i] * 9; else result[i+1] = result[i]; } result[i+1] %= max; } return result[len]; } bool canCombine(char a, char b) { if(a == '1') return true; if(a == '2' && b >= '0' && b <= '6') return true; return false; }};