LeetCode 172. Factorial Trailing Zeroes

来源:互联网 发布:淘宝付款显示系统异常 编辑:程序博客网 时间:2024/05/19 15:44

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

Note: Your solution should be in logarithmic time complexity.


I, in general, don't like the math programming questions... they are pretty tricky but would be very easy once you rule it.

This one actually is an exceptional....

    int trailingZeroes(int n) {        int count = 0;        for(long long int i = 5; i <= n; i = i * 5) {            count += n / i;        }        return count;    }

Do it recursively.... so neat!

public int trailingZeroes(int n) {    return n>=5 ? n/5 + trailingZeroes(n/5): 0;}


0 0
原创粉丝点击