Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
class Solution {
public:
int trailingZeroes(int n) {
int power = 1;
int c = 0;
int f = (int) (n/(pow(5, power)));
while(f > 0){
c += f;
f = (int) (n/(pow(5, ++power)));
}
return c;
}
};
本文介绍了一种计算任意整数n的阶乘(n!)中末尾0的数量的方法,并提供了一个采用对数时间复杂度的C++实现方案。通过不断地除以5的幂次来累加结果。
1237

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



