题目

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

Note: Your solution should be in logarithmic time complexity.

Credits:

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

分析

题目描述:给定一个整数n,求对于n!末尾0的个数。

开始看到的时候并没有什么思路,只知道n!=1∗2∗3∗...∗n

那么末尾0是怎么产生的呢,必然是 质因数 2∗5而导致的结果 , 又搜索了网上一些资料:

对n!做质因数分解n!=2x∗3y∗5z∗...

显然0的个数等于min(x,z),并且min(x,z)==z

证明:

对于阶乘而言,也就是1∗2∗3∗...∗n

[n/k]代表1−n中能被k整除的个数

那么很显然

[n/2]>[n/5](左边是逢2增1,右边是逢5增1)

[n/22]>[n/52](左边是逢4增1,右边是逢25增1)

……

[n/2p]>[n/5p](左边是逢2p增1,右边是逢5p增1)

随着幂次p的上升,出现2p的概率会远大于出现5p的概率。

因此左边的加和一定大于右边的加和,也就是n!质因数分解中,2的次幂一定大于5的次幂

此时,便很明了了,结果可以表示为:

n!后缀0的个数 = n!质因子中5的个数 = floor(n/5)+floor(n/25)+floor(n/125)+....

AC代码

class Solution {
public:
int trailingZeroes(int n) {
int count = 0;
while (n)
{
count += n / 5;
n /= 5;
}
return count;
}
};

LeetCode(172)Factorial Trailing Zeroes的更多相关文章

  1. LeetCode(73)Set Matrix Zeroes

    题目 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cli ...

  2. LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)

    172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...

  3. 【LeetCode】172. Factorial Trailing Zeroes

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

  4. LeetCode Day4——Factorial Trailing Zeroes

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

  5. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

  6. LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

    数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...

  7. LeetCode_172. Factorial Trailing Zeroes

    172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...

  8. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  9. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

随机推荐

  1. 牛客网Java刷题知识点之子类继承不了父类里的(private属性、private方法、构造方法)

    不多说,直接上干货! 子类可以继承父类的属性和方法,除了那些private的外还有一样是子类继承不了的---构造器.对于构造器而言,它只能够被子类调用,而不能被子类继承. 调用父类的构造方法我们使用s ...

  2. 物体检测丨从R-CNN到Mask R-CNN

    这篇blog是我刚入目标检测方向,导师发给我的文献导读,深入浅出总结了object detection two-stage流派Faster R-CNN的发展史,读起来非常有趣.我一直想翻译这篇博客,在 ...

  3. 求一个极大数的欧拉函数 phi(i)

    思路: 因为当n>=1e10的时候,线性筛就不好使啦.所以要用一个公式 φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn) 证明详见:<公式 ...

  4. vue2.0:(七)、vue-resource

    本篇文章开始前,先介绍下什么是vue-resource,并且现在还有一个axios. Vue.js是数据驱动的,这使得我们并不需要直接操作DOM,如果我们不需要使用jQuery的DOM选择器,就没有必 ...

  5. Rest_framework之版本控制、响应器和分页器

    一.访问频率补充 频率: 自定义: 1 定义一个类MyThrottles allow_request(频率限制的逻辑) ==>这两个函数都是派生出来的,继承的类里面封装的. wait(返回一个数 ...

  6. ubuntu下irobot串口通讯

    在window下以前就`有一个现成的串口代码.想移植到ubuntu下,发现都不一样了.要重新找个. 折腾了一上午之后,发现自己写这个串口通讯还是有一点难度. 于是,用了github上 Erick Co ...

  7. gitinore修改不生效

    .gitignore只能忽略那些尚未被被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的.一个简单的解决方法就是先把本地缓存删除(改变成未track状态),然后 ...

  8. 中国区 Azure 服务和定价模式概述

    由世纪互联运营的 Microsoft Azure 是第一个在中国正式商用,符合中国政府相关法规要求的国际化公有云服务.本文剖析了由世纪互联运营的 Microsoft Azure 的运营模式.采购模式. ...

  9. SequenceFile和MapFile

    HDFS和MR主要针对大数据文件来设计,在小文件处理上效率低.解决方法是选择一个容器,将这些小文件包装起来,将整个文件作为一条记录,可以获取更高效率的储存和处理,避免多次打开关闭流耗费计算资源.hdf ...

  10. pc端常见布局---垂直居中布局 单元素不定高

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...