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

Example 1:

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

Example 2:

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

Note: Your solution should be in logarithmic time complexity.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

这道题并没有什么难度,是让求一个数的阶乘末尾0的个数,也就是要找乘数中 10 的个数,而 10 可分解为2和5,而2的数量又远大于5的数量(比如1到 10 中有2个5,5个2),那么此题即便为找出5的个数。仍需注意的一点就是,像 25,125,这样的不只含有一个5的数字需要考虑进去,参加代码如下:

C++ 解法一:

  1. class Solution {
  2. public:
  3. int trailingZeroes(int n) {
  4. int res = ;
  5. while (n) {
  6. res += n / ;
  7. n /= ;
  8. }
  9. return res;
  10. }
  11. };

Java 解法一:

  1. public class Solution {
  2. public int trailingZeroes(int n) {
  3. int res = 0;
  4. while (n > 0) {
  5. res += n / 5;
  6. n /= 5;
  7. }
  8. return res;
  9. }
  10. }

这题还有递归的解法,思路和上面完全一样,写法更简洁了,一行搞定碉堡了。

C++ 解法二:

  1. class Solution {
  2. public:
  3. int trailingZeroes(int n) {
  4. return n == ? : n / + trailingZeroes(n / );
  5. }
  6. };

Java 解法二:

  1. public class Solution {
  2. public int trailingZeroes(int n) {
  3. return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
  4. }
  5. }

Github 同步地址:

https://github.com/grandyang/leetcode/issues/172

类似题目:

Number of Digit One

Preimage Size of Factorial Zeroes Function

参考资料:

https://leetcode.com/problems/factorial-trailing-zeroes/

https://leetcode.com/problems/factorial-trailing-zeroes/discuss/52371/My-one-line-solutions-in-3-languages

https://leetcode.com/problems/factorial-trailing-zeroes/discuss/52373/Simple-CC%2B%2B-Solution-(with-detailed-explaination)

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数的更多相关文章

  1. [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  2. LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  3. [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数

    LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...

  4. 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)

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

  5. Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

    题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  6. 172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  7. ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  8. Java [Leetcode 172]Factorial Trailing Zeroes

    题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  9. Leetcode 172 Factorial Trailing Zeroes

    给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...

随机推荐

  1. QuantLib 金融计算——自己动手封装 Python 接口(1)

    目录 QuantLib 金融计算--自己动手封装 Python 接口(1) 概述 QuantLib 如何封装 Python 接口? 自己封装 Python 接口 封装 Array 和 Matrix 类 ...

  2. linux软链接

    这是linux中一个非常重要的命令,他的功能是为某一个文件在另一个位置建立一个同步的链接,这个命令最常用的参数是-s, 具体用法是: ln -s 源文件 目标文件 当我们需要在不同的目录,用到相同的文 ...

  3. Vue.js 源码分析(十九) 指令篇 v-html和v-text指令详解

    双大括号会将数据解释为普通文本,而非 HTML 代码.为了输出真正的 HTML,你需要使用 v-html 指令,例如: <!DOCTYPE html> <html lang=&quo ...

  4. fatal error compiling: tools.jar not found

    在Eclipse中使用Maven提供的Install(打包)命令插件的时候报错[Fatal error compiling: tools.jar not found]. 报错的原因 报错的原因从错误信 ...

  5. java函数式编程的形式

    java中没有真正的函数变量: 一.所有的函数(拉姆达)表达式,都被解释为functional interface @FunctionalInterface interface GreetingSer ...

  6. VS2013(InstallShield2015LimitedEdition)打包程序详解

    VS2012没有自带打包工具,所以要先下载并安装一个打包工具.我采用微软提供的打包工具:  InstallShield2015LimitedEdition.下载地址:https://msdn.micr ...

  7. Review: Basic Knowledge about WebForm

    Asp.net shanzm

  8. Eureka源码解析系列文章汇总

    先看一张图 0 这个图是Eureka官方提供的架构图,整张图基本上把整个Eureka的核心功能给列出来了,当你要阅读Eureka的源码时可以参考着这个图和下方这些文章 EurekaServer Eur ...

  9. java 图书馆初级编写

    import java.util.Scanner; import java.util.Arrays; public class book { public static void main(Strin ...

  10. i春秋CTF-“百度杯”CTF比赛 九月场 XSS平台

    “百度杯“CTF比赛 九月场 ###XSS平台   看了别人的wp才知道这里需要变数组引起报错然后百度信息收集,这一步在实战中我觉得是很有作用的,get到.       这里取百度rtiny,看别人w ...