您的位置:首页 > 编程语言 > Java开发

Java for LeetCode 172 Factorial Trailing Zeroes

2015-06-06 00:53 441 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: