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’ -> 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:
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的值)

result[i+1]=kresult[i]+lresult[i1]

而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;    }};
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 鼻梁扭曲 翘鼻梁 提升鼻梁 鼻梁大 鼻梁太塌 弄高鼻梁 鼻梁张斑 没有鼻梁 鼻梁骨折会自己恢复吗 如何判断鼻梁塌不塌 鼻梁发育黄金时间 鼻梁在哪个位置图解 宝宝鼻梁上有青筋 鼻梁骨骨折属于什么伤 鼻梁骨一摁就疼正常吗 高鼻梁的标准 鼻梁高的女人 塌鼻梁怎么变高 高鼻梁怎么捏 鼻梁高的男人 鼻梁骨折要赔偿多少钱 小孩鼻梁有青筋推拿手法图 婴儿鼻梁上有青筋 鼻梁上有青筋 宝宝鼻梁有青筋图片 婴儿鼻梁发青 鼻梁凹陷怎么办 高鼻梁的方法 鼻梁上有痣代表什么 怎样让鼻梁骨变高 为什么鼻梁塌 怎样变高鼻梁 怎样拥有高鼻梁 如何让鼻梁变得高挺 怎么使鼻梁变挺 怎样使鼻梁增高 婴儿鼻梁塌怎么办 怎么算高鼻梁 整容垫鼻梁多少钱 塌鼻梁整形多少钱 高鼻梁好看吗