Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
思路:实现很简单,关键的分析题意,要求n!末尾的0数,事实上可以求5和2的对数。而又因为出现5必定出现2,所以求5个数即可.
int trailingZeroes(int n) {
int count = 0;
n = n/5;
while(n>0)
{
count+=n;
n /=5;
}
return count;
}
本文介绍了一种高效计算n!尾部零的数量的方法。利用数学观察到每对5和2相乘都会产生一个额外的0,而5比2更稀缺,因此只需要计算n!中5因子的数量即可。
512

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



