172. Factorial Trailing Zeroes

来源:互联网 发布:it培训学校排名 编辑:程序博客网 时间:2024/05/17 01:10

题目

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.


思路

计算n的阶乘的最后有几个零,就是计算因数中2和5的最小个数,5的个数肯定比2的少,就是计算因数中5的个数


代码

class Solution {public:    int trailingZeroes(int n) {        //计算n的阶乘的最后有几个零,就是计算因数中2和5的最小个数,5的个数肯定比2的少,就是计算因数中5的个数        if(n <= 4)        {            return 0;        }        int times = 0;        while(n)        {            times += n/5;            n /= 5;        }        return times;    }};
0 0
原创粉丝点击