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

Note: Your solution should be in logarithmic time complexity.

解题思路:

计算n能达到的5的最大次幂,算出在这种情况下能提供的5的个数,然后减去之后递归即可,JAVA实现如下:

	static public int trailingZeroes(int n) {
if(n<25)
return n/5;
long five=5;
int count=0;
while(n>=five){
five*=5;
count++;
}
int temp=(int) (n/Math.pow(5, count));
return countSum(count)*temp+trailingZeroes(n-temp*(int)Math.pow(5, count));
}
static public int countSum(int count){
if(count==1)
return 1;
else return countSum(count-1)*5+1;
}

Java for LeetCode 172 Factorial Trailing Zeroes的更多相关文章

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

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

  3. Java [Leetcode 172]Factorial Trailing Zeroes

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

  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. 后台管理UI推荐

    目录 一.EasyUI 二.DWZ JUI 三.HUI 四.BUI 五.Ace Admin 六.Metronic 七.H+ UI 八.其它UI 九.总结 最近要做一个企业的OA系统,以前一直使用Eas ...

  2. android HDMI 清晰度 分辨率

    但改变分辨率时,发送广播即可: Intent intent_outputmode_change = new Intent(ACTION_OUTPUTMODE_CHANGE);     intent_o ...

  3. android 自定义控件 使用declare-styleable进行配置属性(源码角度)

          android自定义styleableattrs源码 最近在模仿今日头条,发现它的很多属性都是通过自定义控件并设定相关的配置属性进行配置,于是便查询了解了下declare-styleabl ...

  4. POJ1651Multiplication Puzzle(矩阵链乘变形)

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8040   Accepted: ...

  5. POJ2586Y2K Accounting Bug(贪心 + 不好想)

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12251   Accepted: 62 ...

  6. Yii2 如何使用事件

    原文地址:http://www.fancyecommerce.com/2016/04/29/yii2-%E4%BD%BF%E7%94%A8event-1-%EF%BC%8C%E5%A6%82%E4%B ...

  7. ajax局部更新

    js //点击启用 $(".status").on("click",function(){ var id = $(this).attr("status ...

  8. MySql数据类型详解

    可配合http://www.cnblogs.com/langtianya/archive/2013/03/10/2952442.html学习 MySql数据类型 1.整型(xxxint)   MySQ ...

  9. Office Web Apps资源

    http://www.cnblogs.com/poissonnotes/p/3277280.html#!comments http://www.cnblogs.com/poissonnotes/p/3 ...

  10. thinkphp 3.23 第三方登录sdk集成包

    本集成包在官方包上扩展了支付宝登录和微信,支持最新的3.23版本 config    /* URL配置 */     'URL_CASE_INSENSITIVE' => true, //默认fa ...