思路:动态规划,dp的i + 1位存储nums的i字符可翻译成字符串的个数
1、如果当前位可以翻译成1-9(a-i),dp[i+1] += dp[i] 2、如果当前位位10-26的个位(j-z),dp[i+1] += dp[i-1]
class Solution:
def solve(self , nums: str) -> int:
# write code here
if len(nums) == 0:
return 0
dp = [0] * (len(nums) + 1)
dp[0] = 1
for i in range(len(nums)):
if nums[i] != '0':
dp[i+1] += dp[i]
if i > 0 and nums[i-1] != '0' and int(nums[i-1:i+1]) <= 26:
dp[i+1] += dp[i-1]
return dp[-1]