Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
求階乘的后綴0個數乘法中的零來源于10,10來源于2和5,在階乘中,1個數的質因子出現1次5,那末必定有其他數的質因子出現若干次2
所以問題變成求解質因子5出現的次數,
n/5求出包括1個5的數字個數
n/25求出包括兩個5的數字個數...以此類推
class Solution { public: int trailingZeroes(int n) { int res = 0; while(n) { res += n/5; n/=5; } return res; } };
上一篇 樸素貝葉斯