91. Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:1234'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.
这种题目也挺恶心的,就是分情况讨论,先把情况分的细一点没关系,最后可以把条件合并
譬如说可以分成
- 00 这种情况不可能,真接return 0
- 01 - 09 这种情况0只能跟前面的配. dp[i] = dp[i - 1]
- 10 10只能配在一起 dp[i] = dp[i - 2]
- 11 - 19 十位即可以跟前面的配,也可以跟后面的配. dp[i] = dp[i - 1] + dp[i - 2]
- 20 20只能配在一起 dp[i] = dp[i - 2]
- 21 - 26 十位即可以跟前面的配,也可以跟后面的配. dp[i] = dp[i - 1] + dp[i - 2]
- 27+ 这种情况,十位只能跟前面的配. dp[i] = dp[i - 1]
- 30, 40, …. 这种情况不可能,return 0
|
|