Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
solution:
zero comes from 2*5, and number of 2 is less than 5. So we can only count the number of 5 contained in n!.
public int trailingZeroes(int n) {
if(n<5) return 0;
int count = 0;
while(n/5 !=0){
n/=5;
count +=n;
}
return count;
}

本文介绍了一种高效算法,用于计算给定整数n的阶乘(n!)中末尾零的数量。通过聚焦于计数阶乘中5的因子数量,该算法实现了对数时间复杂度,提供了一个简洁且高效的解决方案。
7657

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



