力扣: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
相关推荐
老赵全栈实战18 分钟前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋6 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
Wect7 小时前
LeetCode 130. 被围绕的区域:两种解法详解(BFS/DFS)
前端·算法·typescript
NAGNIP18 小时前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
颜酱1 天前
单调栈:从模板到实战
javascript·后端·算法