题目:
给定一个整数
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
相关推荐
ZPC82101 天前
docker 镜像备份ZPC82101 天前
docker 使用GUI ROS2琢磨先生David1 天前
Day1:基础入门·两数之和(LeetCode 1)颜酱1 天前
栈的经典应用:从基础到进阶,解决LeetCode高频栈类问题多恩Stone1 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系生信大杂烩1 天前
癌症中的“细胞邻域“:解码肿瘤微环境的空间密码 ——Nature Cancer 综述解读QQ4022054961 天前
Python+django+vue3预制菜半成品配菜平台百锦再1 天前
Django实现接口token检测的实现方案QQ5110082851 天前
python+springboot+django/flask的校园资料分享系统QQ_19632884751 天前
Python-flask框架西山区家政服务评价系统网站设计与开发-Pycharm django