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

把数字翻译成字符串

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

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 解码
# @param nums string字符串 数字串
# @return int整型
#
class Solution:
    def solve(self , nums: str) -> int:
        # write code here
        count=[]
        for i in range(len(nums)):
            if i==0:
                if int(nums[i])>0:
                    count.append(1)
                else:
                    count.append(0)
            elif i==1:
                if 0<int(nums[i-1:i+1])<=26 and int(nums[i])!=0:
                    count.append(2)
                else:
                    count.append(1)
            else:
                if 10<=int(nums[i-1:i+1])<=26 and int(nums[i])!=0:
                    count.append(count[i-2]+count[i-1])
                elif int(nums[i])==0 and 10<=int(nums[i-1:i+1])<=26:
                    count.append(count[i-2])
                elif int(nums[i])==0 :
                    return 0
                else:
                    count.append(count[i-1])
        return count[-1]
全部评论

相关推荐

牛至超人:哈工大已经很棒了,不需要加括号了,然后咋没有实习经历呢?火速趁寒假整一段实习,导师不让就狠狠肘击
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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