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

把数字翻译成字符串

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]

全部评论

相关推荐

后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务