
给定一个整数 n,返回 n! 结果尾数中零的数量。
示例 1:
输入: 3
输出: 0
解释: 3! = 6, 尾数中没有零。
示例 2:
输入: 5
输出: 1
解释: 5! = 120, 尾数中有 1 个零.
说明: 你算法的时间复杂度应为 O(log n) 。class Solution {
public:
int trailingZeroes(int n) {
int ans = 0;
for(int i = 5;i <= n;i += 5){
int t = i;
while(!(t % 5)){
ans ++;
t /= 5;
}
}
return ans;
}
};class Solution {
public:
int trailingZeroes(int n) {
int ans = 0;
for(int i = 5;i <= n;i *= 5){
ans += n / i;
}
return ans;
}
};发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/168543.html原文链接:https://javaforall.cn