Decode Ways(dp)

A message containing letters fromA-Zis being encoded to numbers using the following mapping:

'A' -
>
 1
'B' -
>
 2
...
'Z' -
>
 26

Given anon-emptystring containing only digits, determine the total number of ways to decode it.

Example 1:

Input:
 "12"

Output:
 2

Explanation:
 It could be decoded as "AB" (1 2) or "L" (12).

Example 2:

分析

从i-1或者i-2到达当前I

记得判断>0 和>9 and <=26 还有01的情况

https://leetcode.com/problems/decode-ways/discuss/187032/Java-DP%3A-memoization-top-down-and-bottom-up

和2 的区别:当前dp[i-1]到dp[i] 1 就一个str,等于是 dp[i-1]*1

对于2 中间好几种str,所以是dp[i-1]*count(str)

Last updated

Was this helpful?