n!就是1*2*3*4*.......*n,没出现一个5,0就增加一个,比如,103!=1*2*3*4*5*6*7*........*10*.........*103,所所以至少出现5,10,15,20.。。。。100,除以5又得,1,2,3,4,5,。。。。20这又是四个,依次往下,即可
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
public class Solution {
public int trailingZeroes(int n) {
int result = 0;
if(n <= 4){
return 0;
}
while(n/5 != 0){
n/=5;
result += n;
}
return result;
}
}
本文介绍了一种高效计算n!尾部零的数量的方法,并提供了一个简洁的Java代码实现。该算法采用逐步除以5的方式,确保了在对数时间内完成计算。
729

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



