website/content/ChapterFour/0100~0199/0172.Factorial-Trailing-Zeroes.md
Given an integer n, return the number of trailing zeroes in n!.
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.
给定一个整数 n,返回 n! 结果尾数中零的数量。说明: 你算法的时间复杂度应为 O(log n) 。
min(阶乘中 5 的个数和 2 的个数)。res = N/5 + N/(5^2) + N/(5^3) + ... = ((N / 5) / 5) / 5 /... 。最终算法复杂度为 O(logN)。
package leetcode
func trailingZeroes(n int) int {
if n/5 == 0 {
return 0
}
return n/5 + trailingZeroes(n/5)
}