【LeetCode】#172 Factorial Trailing Zeroes(C#)

● 問題

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

Note: Your solution should be in logarithmic time complexity.


● 思考點

求出n!共出現幾個0,時間複雜度需要考慮進去O(logn),這題主要找出5出現幾次(因任一偶數乘以5將會出現0)。


● Solution

public class Solution {
    public int TrailingZeroes(int n) {
        
        int Result = 0;
        
        while(n >= 5)
        {
            n /= 5;
            Result += n;
        }
        
        return Result;
    }
}