【题目描述】
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
【思路】看n的阶乘里有多少个0,只要看1~n中有多少个2和5就可以了,又因为2肯定比5多,所以只要看5有多少个就可以了。另外,还要注意像25里包含了2个5,125里包含了3个5.。。
【代码】
class Solution {
public:
int trailingZeroes(int n) {
int ans=0;
while(n){
ans+=n/5;
n=n/5;
}
return ans;
}
};