天天看點

LeetCode 172. Factorial Trailing Zeroes(0結尾)

原題網址:https://leetcode.com/problems/factorial-trailing-zeroes/

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

Note: Your solution should be in logarithmic time complexity.

方法:0的個數由5決定。

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