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

把数字翻译成字符串

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

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 解码
# @param nums string字符串 数字串
# @return int整型
#
class Solution:
    def con(self,last,this):
        c = [0,0]
        if 0<int(this)<=9:
            c[0]=1
        if 10<=int(last+this)<=26:
            c[1]=1
        return c

    def solve(self , nums: str) -> int:
        # write code here
        leng = len(nums)
        dp = [0 for i in range(leng)]
        if int(nums[0])==0:
            return 0
        dp[0] = 1
        for i in range(1,leng):
            this = nums[i]
            if i==1:
                last = nums[0]
                count = self.con(last,this)
                dp[i] = sum(count)
            else:
                last = nums[i-1]
                count = self.con(last,this)
                dp[i] = count[0]*dp[i-1] + count[1]*dp[i-2]
                    
        return dp[-1]

全部评论

相关推荐

2024-12-30 22:49
长沙理工大学 Java
神哥了不得:没什么可以指导的地方了,简历确实牛,我大号分享过投递策略,广投就行
点赞 评论 收藏
分享
神哥了不得:放平心态,再找找看吧,主要现在计算机也变卷了,然后就比较看学历了,之前高中毕业你技术强,都能找到工作的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务