力扣:172. 阶乘后的零(Python3)

题目:

给定一个整数 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个数。

代码:

python 复制代码
from 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
相关推荐
智能体与具身智能8 小时前
TVA具身智能的概念、架构与应用(19)
人工智能·python·具身智能
2601_962294619 小时前
python中range函数怎么用
python·for循环·可迭代对象·range函数·整数列表
青 春 记 忆10 小时前
零基础入门python70:Docker Compose 编排完整后端
python·后端开发
新时代牛马10 小时前
字符设备驱动完整篇:从 cdev_add、file_operations 到chrdev_open 与排障
开发语言·python
政企项目老覃11 小时前
大模型幻觉治理与自动评测:金融风控场景的落地实践
人工智能·算法·机器学习
淡海水11 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
白山编程大哥11 小时前
Java OutputStreamWriter 详解:从字符到字节的桥梁
java·开发语言·python
hansang_IR11 小时前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论
洛阳纸贵12 小时前
MATLAB-matlab基础知识
学习·算法·matlab