Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
思路:有多少个2与5相乘,就有多少个0,由于2远多于5,就是算有多少个5。
class Solution {
public:
int trailingZeroes(int n) {
int len = 0;
while(n >= 5)
{
len = len + n/5;
n = n/5;
}
return len;
}
};
本文介绍了一种快速计算阶乘尾部零的个数的方法,通过计算阶乘中5的倍数的个数来确定零的个数,利用对数时间复杂度实现高效的计算。
1610

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



