题目:
给定一个整数
n,返回n!结果中尾随零的数量。提示
n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1来源:力扣(LeetCode)
链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
示例:
示例 1:
输入:n = 3
输出:0
解释:3! = 6 ,不含尾随 0
示例 2:
输入:n = 5
输出:1
解释:5! = 120 ,有一个尾随 0
示例 3:
输入:n = 0
输出:0
解法:
使用math.factorial函数求阶乘,统计结果中尾0个数。
代码:
pythonfrom math import factorial class Solution: def trailingZeroes(self, n: int) -> int: count = 0 f = factorial(n) while f % 10 == 0: count += 1 f //= 10 return count
力扣:172. 阶乘后的零(Python3)
恽劼恒2023-11-21 9:39
相关推荐
geovindu39 分钟前
python:DecisionTree怒放的生命19912 小时前
代码知识库 RAG 问答系统 —— 从 0 到 1 实现教程geovindu2 小时前
python: Decision Tree Regressorfengkai45453 小时前
Python运维Fancy.7613 小时前
:Python条件与循环语句学习总结AllData公司负责人4 小时前
实战二:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 Http 接口同步至 Doris,API 业务数据自动拉取入数仓Wellijohn5 小时前
Python 容器选型实测:list.insert(0) 慢 925 倍,in 慢 6000 倍新*5 小时前
标题全变成一级?靠编号规律找回章节边界武子康5 小时前
火系被选七成,就应该削弱吗?