题解 | #把数字翻译成字符串#

把数字翻译成字符串

https://www.nowcoder.com/practice/046a55e6cd274cffb88fc32dba695668

class Solution:
    def solve(self, nums: str) -> int:
        n = len(nums)
        if n == 0:
            return 0

        dp = [0] * (n + 1)
        dp[0] = 1
        dp[1] = 1 if nums[0] != "0" else 0

        for i in range(2, n + 1):
            if nums[i - 1] != "0":
                dp[i] += dp[i - 1]
            if "10" <= nums[i - 2 : i] <= "26":
                dp[i] += dp[i - 2]

        return dp[n]

全部评论

相关推荐

阿里国际 icbu n*16
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务