您的位置:首页 > 大数据 > 人工智能

Leetcode 172 Factorial Trailing Zeroes

2017-01-11 15:03 393 查看
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,在阶乘中,一个数的质因子出现一次5,那么必然有其他数的质因子出现若干次2

所以问题变为求解质因子5出现的次数,

n/5求出包含一个5的数字个数

n/25求出包含两个5的数字个数...以此类推

class Solution {
public:
int trailingZeroes(int n) {
int res = 0;
while(n)
{
res += n/5;
n/=5;
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: