码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode //C - 172. Factorial Trailing Zeroes


    172. Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!.

    Note that n! = n * (n - 1) * (n - 2) * … * 3 * 2 * 1.
     

    Example 1:

    Input: n = 3
    Output: 0
    Explanation: 3! = 6, no trailing zero.

    Example 2:

    Input: n = 5
    Output: 1
    Explanation: 5! = 120, one trailing zero.

    Example 3:

    Input: n = 0
    Output: 0

    Constraints:
    • 0 < = n < = 1 0 4 0 <= n <= 10^4 0<=n<=104

    From: LeetCode
    Link: 172. Factorial Trailing Zeroes


    Solution:

    Ideas:
    • We initialize count to 0 to keep track of the number of 5s.
    • We use a while loop that runs as long as n is greater than 0.
    • Inside the loop, we divide n by 5 to find out how many multiples of 5, 25, 125, etc., are there in n.
    • We add this value to count.
    • Finally, we return count, which represents the number of trailing zeroes in n!.
    Code:
    int trailingZeroes(int n) {
        int count = 0;
        while (n > 0) {
            n /= 5;
            count += n;
        }
        return count;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    01-Linux
    flink集群部署
    旋转框目标检测mmrotate v0.3.1 学习模型
    第3章 定义内存缓存和log4net日志中间件
    java计算机毕业设计政府资源交易中心业务流程管理系统设计与实现源码+数据库+系统+部署+lw文档
    拼接SQL语句的优雅姿势
    VoLTE基础自学系列 | IMS的业务触发机制
    剪拼子字符串——常规、另类两种算法解CSDN竞赛第八期第一小题
    【从零开始学习 SystemVerilog】8.17、SystemVerilog 约束—— Disable Randomization(关闭随机)
    “富二代”极氪,辉煌过后总要独自长大
  • 原文地址:https://blog.csdn.net/navicheung/article/details/134499090
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号