LeetCode 91. Decode Ways
题目描述:
A message containing letters from
A-Z
is being encoded to numbers using the following mapping:
1
2
3
4
5 'A' -> 1
'B' -> 2
...
'Z' -> 26Given 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.
动态规划, 最后一个字母可以是一位数字或者两位数字. 在最后一位不是0时可以是一位数字, 在倒数第二位为1, 或倒数第二位为2且最后一位小于等于6时可以为两位数字, 解码的不同方法数量为这两种情况之和.
1 | class Solution { |