栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

leetcode每日一题—639.解码方法II

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode每日一题—639.解码方法II

题目



思路
见官方题解

解答

class Solution:
 def numDecodings(self, s: str) - int:
 mod 10**9 7
 def check1digit(ch: str) - int:
 if ch 0 :
 return 0
 return 9 if ch * else 1
 def check2digits(c0: str, c1: str) - int:
 if c0 * and c1 * :
 return 15
 if c0 * :
 return 2 if c1 6 else 1
 if c1 * :
 return 9 if c0 1 else (6 if c0 2 else 0)
 return int(c0 ! 0 and int(c0) * 10 int(c1) 26)
 n len(s)
 # a f[i-2], b f[i-1], c f[i]
 a, b, c 0, 1, 0
 for i in range(1, n 1):
 #print(i,a,b,c)
 #使用一位进行编码
 c b * check1digit(s[i - 1])
 #使用两位进行编码
 if i 1:
 c a * check2digits(s[i - 2], s[i - 1])
 c % mod
 return c
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/266642.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号