[LeetCode]Factorial Trailing Zeroes

来源:互联网 发布:林弯弯的淘宝店叫什么 编辑:程序博客网 时间:2024/06/07 09:02

Given an integer n, return the number of trailing zeroes inn!.

Note: Your solution should be in logarithmic time complexity.

求n的阶乘后面有几个零,要求时间复杂度O(logn)。

刚看到这个题有点蒙,后来想了想,零是怎么增加的,只有10*10才会增加,阶乘中从不缺偶数,所以n中有几个5的因子就有几个零,5!只有1个零,10!有2个零,11-14也只有2个零,15!就有三个零了。

int trailingZeroes(int n) {        int num = 0;        while(n)        {            n /= 5;            num += n;        }        return num;    }
时间复杂度O(logn) ,响应时间0ms。

0 0
原创粉丝点击