题目:Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
计算n的阶乘的拖尾0的个数,即n中5的倍数的个数。One:
public class Solution {
public int trailingZeroes(int n) {
if(n<0){
return 0;
}
int c = 0;
while(n/5!=0){
n/=5;
c+=n;
}
return c;
}
}Two:递归
public int trailingZeroes(int n) {
return n>=5 ? n/5 + trailingZeroes(n/5): 0;
}
本文介绍了一种高效算法来计算任意整数n的阶乘末尾0的数量。该算法利用了数学原理,通过迭代或递归的方式,在对数时间内找到结果。文中提供了两种实现方法。
729

被折叠的 条评论
为什么被折叠?



