Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.算法
給定一個整數n,求階乘結果的尾部0的個數。spa
假如i能夠被5整除,則能夠提供的5的個數爲i/5個
假如i能夠被25整除,則能夠多提供的5的個數爲i/25個
假如i能夠被125整除,則能夠多提供的5的個數爲i/125個(算上了被5,25整除以後)
。。。.net
算法實現類code
public class Solution { // 假如i能夠被5整除,則能夠提供的5的個數爲i/5個 public int trailingZeroes(int n) { int result = 0; long tmp = n; // 爲了防止i*5超出int的表大表示範圍 for (long i = 5; i <= tmp; i *= 5) { result += n / i; } return result; } }