172 Factorial Trailing Zeroes ***** 不可思议!!!

2 *5 = 10

10 * 9 * 8 * 7``````

出现5的次数一定高于2 的次数,所以只需要看出现几个5

比如 5 *4----- = 120 ->1 ci

10 ! = 3628800 -> 2

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

Example 1:

Input:
 3

Output:
 0

Explanation:
 3! = 6, no trailing zero.

Example 2:

Input:
 5

Output:
 1

Explanation:
 5! = 120, one trailing zero.

Note:Your solution should be in logarithmic time complexity.

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

results matching ""

    No results matching ""