开始刷题leetcode day3:Factorial Trailing Zeroes

来源:互联网 发布:mdf文件恢复数据库 编辑:程序博客网 时间:2024/06/12 23:06

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

Note: Your solution should be in logarithmic time complexity.



Java:

public class Solution {
    public int trailingZeroes(int n) {
        int k = 0;
        if(n<0)return 0;
        for(long i = 5; i<=n; i*=5)
        {
            k+= n/i;
        }
        return k;
    }
}


注意需要用long i

具体方法参考了网上,只需要数5,25,125.。。。。(2的次数会比5的多,所以只需要数5)

0 0