題目:172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
n!得到的數(shù)末尾有多少零。
2 * 5 = 10 帶來一個0村象;所以只需計算n球恤!里的25的pair卷谈。2很多(雙數(shù)都可以)所以計算5得出pair數(shù)笤妙,即n/5盗舰。
然而考慮5的n次冪便监,以25為例扎谎,是55,也就是可以分別和兩個2相乘得到0烧董,所以實際要計算n/5/5...直到商為0毁靶,即不能被5整除。結(jié)果為n/5+n/5/5...之和
class Solution {
public int trailingZeroes(int n) {
int res = 0;
while (n/5 !=0){
res += n/5;
n /= 5;
}
return res;
}
}