172. 阶乘后的零

172. Factorial Trailing Zeroes

题目描述

给定一个整数 n,返回 n! 结果尾数中零的数量。

LeetCode172. Factorial Trailing Zeroes

示例 1:

输入: 3
输出: 0
解释: 3! = 6,尾数中没有零。

示例 2:

输入: 5
输出: 1
解释: 5! = 120,尾数中有 1 个零。

说明: 你算法的时间复杂度应为 O(log n)。

Java 实现

递归

class Solution {
public static int trailingZeroes(int n) {
return n < 5 ? 0 : n / 5 + trailingZeroes(n / 5);
}
}

迭代

class Solution {
public static int trailingZeroes(int n) {
int result = 0;
while (n > 0) {
result += n / 5;
n /= 5;
}
return result;
}
}

参考资料

LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)的更多相关文章

  1. [Swift]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  2. Java实现 LeetCode 172 阶乘后的零

    172. 阶乘后的零 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! ...

  3. Leetcode 172.阶乘后的零

    阶乘后的零 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! = 120 ...

  4. 【每天一题】LeetCode 172. 阶乘后的零

    开源地址:点击该链接 题目描述 https://leetcode-cn.com/problems/factorial-trailing-zeroes 给定一个整数 n,返回 n! 结果尾数中零的数量. ...

  5. 172. 阶乘后的零 Java解法

    https://leetcode-cn.com/problems/factorial-trailing-zeroes/ 172. 阶乘后的零 这题要完成其实要知道一个很巧妙的思想,就是阶乘里面,后面的 ...

  6. leetcode刷题笔记172 阶乘后的零

    题目描述: 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例1: 输入: 输出: 解释: ! = , 尾数中没有零. 示例2: 输入: 输出: 解释: ! = , 尾数中有 个零. 说明: 你 ...

  7. 每日一道 LeetCode (41):阶乘后的零

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  8. 【LeetCode】172. Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

  9. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

随机推荐

  1. 项目管理、bug管理工具 ---禅道使用流程

    使用前描述: 禅道是付费的一款云平台工具,它可以实现项目管理.需求管理.bug提交.bug跟踪.文档管理.bug统计等功能 使用账号.密码:公司提供,登录后基本识别操作流程如下: 1.登录首页-我的地 ...

  2. Java ArrayList几种遍历方法

    import java.util.ArrayList; import java.util.Iterator; public class StringSampleDemo { public static ...

  3. [提权]mysql中的UDF提权

    由于udf提权是需要构造UDF函数文件的,涉及到了写文件.所以本次实验已经将mysql的配置做了改动:–secure-file-priv=''. 剧情须知: secure_file_priv 为 NU ...

  4. 如何画好ER图

    快速阅读 了解ER图的基本组成,以及如何在viso中画ER图. 什么是ER图 是实体关系图,用矩形表示实体,用椭圆形表示属性,用棱形表示两实体之间的联系.相互用直接联接起来,是一种数据建模工具.用来描 ...

  5. web前端兼容性问题

    传送门:https://www.cnblogs.com/zhoudawei/p/7497544.html

  6. Git是怎么Ignore文件的?

    Git is one of the most popular version control systems (VCS) available, especially thanks to hosting ...

  7. 数据库中的blob是什么类型?

    数据库中的blob是什么类型? BLOB (binary large object)----二进制大对象,是一个可以存储二进制文件的容器. 在计算机中,BLOB常常是数据库中用来存储二进制文件的字段类 ...

  8. mycat启动报Unable to start JVM: No such file or directory (2)【转】

    mycat启动失败,查看日志 /mycat/logs/wrapper.log发现如下信息 1  STATUS | wrapper  | 2017/11/22 16:15:17 | --> Wra ...

  9. 关于Kubernetes Master高可用的一些策略

    关于Kubernetes Master高可用的一些策略 Kubernetes高可用也许是完成了初步的技术评估,打算将生产环境迁移进Kubernetes集群之前普遍面临的问题. 为了减少因为服务器当机引 ...

  10. Android Support v4、v7、v13、v14、v17的区别和应用场景

    Android Support v4.v7.v13.v14.v17的区别和应用场景   本文链接:https://blog.csdn.net/Aquarius_Seven/article/detail ...