91. Decode Ways
题目
A message containing letters from
A-Z
is being encoded to numbers using the following mapping:1
2
3
4'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.
大意
用数字表示字母,给定一个数字字符串,计算有多少种字符的表示方式.
答案
1 | class Solution { |
思路
(类比爬梯子问题,是和前面两个状态有关!!!!!!)
使用动态规划,当前的dp和前两个状态有关,且有两个限制条件,第一个条件是当前遍历的字符是否为0,第二个是当前的和前一个的是否可以组成合法字符,然后四种情况分类.
Author: corn1ng
Link: https://corn1ng.github.io/2017/12/24/算法/leetcode91/
License: 知识共享署名-非商业性使用 4.0 国际许可协议