LeetCode : Factorial Trailing Zeroes

来源:互联网 发布:高音 知乎 编辑:程序博客网 时间:2024/06/13 13:53

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

Note: Your solution should be in logarithmic time complexity.

解释:
对n!做质因数分解n!=2x*3y*5z*…

显然0的个数等于min(x,z),并且min(x,z)==z

class Solution {public:    int trailingZeroes(int n) {        int ret = 0;        while(n)        {            ret+=n/5;            n/=5;        }        return ret;    }};
0 0
原创粉丝点击