def numDecodings(s): nums=map(int, list(s)) cmbs=[0]*len(nums) cmbs[0]=0 if nums[0]==0 else 1 if len(nums)==1: return cmbs[-1] sup1=0 if nums[1]==0 else cmbs[0] sup2=0 if 10*nums[0]+nums[1]<10 or 10*nums[0]+nums[1]>26 else 1 cmbs[1]=sup1+sup2 for i in range(2, len(nums)): sup1=0 if nums[i]==0 else cmbs[i-1] sup2=0 if 10*nums[i-1]+nums[i]<10 or 10*nums[i-1]+nums[i]>26 else cmbs[i-2] cmbs[i]=sup1+sup2 return cmbs[-1]
时间: 2024-10-11 00:21:57