题解 | #把数字翻译成字符串#
把数字翻译成字符串
https://www.nowcoder.com/practice/046a55e6cd274cffb88fc32dba695668
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 解码 # @param nums string字符串 数字串 # @return int整型 # class Solution: def solve(self , nums: str) -> int: # write code here dp = [1]*len(nums) if nums == '0': return 0 if nums == '10' or nums == '20': return 1 for i in range(1,len(nums)): if nums[i] == '0' and (nums[i-1] != '1' and nums[i-1] != '2'): return 0 if len(nums) > 1: if nums[1] != '0' and int(nums[0:2]) <= 26: dp[1] = 2 for i in range(2,len(nums)): if nums[i] == '0': dp[i] = dp[i-2] else: if nums[i-1] != '0' and int(nums[i-1:i+1]) <= 26: dp[i] = dp[i-1] + dp[i-2] else: dp[i] = dp[i-1] return dp[len(nums)-1]