Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
int trailingZeroes(int n) {
//返回n!中0的个数,这个题目就是求质数5的个数,2的个数肯定是多余5,剑指offer里面曾出现这个题目,有点难理解几行代码就搞定了,可以细细品味
int sum = 0;
while (n)
{
n /= 5;
sum += n;
}
return sum;
}
};
本文介绍了一种快速计算给定整数阶乘尾部零数量的方法,通过求解质数5的个数实现,适用于对算法效率有较高要求的情景。
729

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



