题目

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. proxy_pass http://127.0.0.1:5000; 502 bad getway

    (13: Permission denied) while connecting to upstream:[nginx] I am working with configuring django pr ...

  2. airodump-ng 界面参数比较详细的解释

    BSSID: AP(access point)的MAC地址,,如果在client section中BSSID显示为"not associated" ,那么意味着该客户端没有和AP连 ...

  3. java中两个map比较

    一 /** * 用map的keySet()的迭代器(性能效率较低) * */ public void compareMap1 (){ Map<String, String> m1 = ne ...

  4. @Primary 使用

    造轮子的一个小小的发现 当一个接口被两个service实现时,controller调用接口实现功能,会报错,提示开发者指定service,官方是建议你使用@Qualifier来区分的,但是,总有另一种 ...

  5. python Fuction 方法的调用

    def display():#无参数 print("No") return # display() def callfun():#调用 print("2") d ...

  6. 在MasterPage中检验session是否存在~

    在母板頁中檢查user是否登入過,這樣就不用在每個頁中去作檢驗.在其Init事件中寫入如下代碼:     protected void ContentPlaceHolder1_Init(object  ...

  7. Code First 2

    在codefirst一中也说了Mapping是实体与数据库的纽带,model通过Mapping映射到数据库,我们可以从数据库的角度来分析?首先是映射到数据库,这个是必须的.数据库里面一般包括表.列.约 ...

  8. 移动端之js控制rem,适配字体

    方法一:设置fontsize 按照iphone 5的适配  1em=10px    适配320 // “()()”表示自执行函数 (function (doc, win) { var docEl = ...

  9. 编译安装php容易出现的问题以及解决办法

    http://crybit.com/20-common-php-compilation-errors-and-fix-unix/

  10. java内存分配(堆、栈、常量池)

    Java内存分配: ◆寄存器:我们在程序中无法控制 ◆栈:存放基本类型的数据和对象的引用,以及成员方法中的局部变量 ◆堆:存放对象本身(成员变量+成员方法的引用) ◆静态域:存放在对象中用static ...