Factorial Trailing Zeroes

来源:互联网 发布:传智播客2017java 编辑:程序博客网 时间:2024/06/05 04:12

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.

Show Tags

Have you met this question in a real interview?

思路 :  cc150 上的原题, 查 5 的个数 +  25 的个数  + 125 的个数

leetcode 用乘积会超时, 于是改成往下除。


public class Solution {    public int trailingZeroes(int n) {        if(n < 5)            return 0;        int ret = 0;        while(n > 0){            ret += n/5;            n /= 5;        }        return ret;    }}


0 0
原创粉丝点击