题目描述:

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

Note: Your solution should be in logarithmic time complexity.

解题思路:

对于阶乘而言,也就是1*2*3*...*n
[n/k]代表1~n中能被k整除的个数
那么很显然
[n/2] > [n/5] (左边是逢2增1,右边是逢5增1)
[n/2^2] > [n/5^2](左边是逢4增1,右边是逢25增1)
……
[n/2^p] > [n/5^p](左边是逢2^p增1,右边是逢5^p增1)
随着幂次p的上升,出现2^p的概率会远大于出现5^p的概率。
因此左边的加和一定大于右边的加和,也就是n!质因数分解中,2的次幂一定大于5的次幂

代码如下:

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

  

Java [Leetcode 172]Factorial Trailing Zeroes的更多相关文章

  1. Java for LeetCode 172 Factorial Trailing Zeroes

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

  2. 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 ...

  3. ✡ 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 ...

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

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

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

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

  6. Leetcode 172 Factorial Trailing Zeroes

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

  7. leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)

    数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...

  8. [LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数

    所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125.. ...

  9. LeetCode Day4——Factorial Trailing Zeroes

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

随机推荐

  1. Eclipse plugin插件开发 NoClassDefFoundError

    Eclipse的每一个plugin都有属于自己的类加载器,这是OSGI架构的基础,每一个plugin项目都是一个bundle,独立运行在各自的运行环境里面,这就造成了开发时和运行时的不同. Eclip ...

  2. epoll 知识总结

    poll/select/epoll 对比 http://www.cnblogs.com/apprentice89/p/3234677.html    ---有待继续学习 http://blog.chi ...

  3. PHP中如何给日期加上一个月 加一周 加一天

    echo   date("Y-m-d",strtotime("+1 month",strtotime("2012-02-04"))); 结果 ...

  4. linux vi修改后如何保存

    linux vi修改后如何保存 按ESC键去到命令模式,然后: :w?? 保存文件但不退出vi :w file 将修改另外保存到file中,不退出vi :w! 强制保存,不推出vi :wq 保存文件并 ...

  5. A const field of a reference type other than string can only be initialized with null Error [duplicate]

    I'm trying to create a 2D array to store some values that don't change like this. const int[,] hiveI ...

  6. linux 踢出用户方法

    linux系统root用户可强制踢制其它登录用户, 首先以root登录以便查看全部的在线用户信息,可用w命令查看登录用户信息 强制踢人命令格式:pkill -kill -t tty 解释: pkill ...

  7. [转]Java数组初始化详解

    一维数组1)   int[] a;   //声明,没有初始化 2)   int[] a=new int[5];   //初始化为默认值,int型为0 3)   int[] a={1,2,3,4,5}; ...

  8. lintcode :最大子数组

    题目:  最大子数组 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6 ...

  9. NGUI中Button与原生2D精灵的混合使用

    一些废话 每一篇的首段都是这个“一些废话”,原因是我太能逼逼了,不逼逼一些废话我就觉得难受.这是我第四篇关于Unity的博文,前两篇还是去年写的,“从一点儿不会开始”系列,类似教程和学习笔记的博文,这 ...

  10. 49. Anagrams

    题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...