Title: (Math)
Given an integer n, return the number of trailing zeroes in N!.
Note:your solution should is in logarithmic time complexity.
Exercises
Let's start by listing a few examples to find out the rules.
and found
5! There's a 0,
10! There are two of 0,
15! There are three of 0,
20! There are four of 0,
25! There are six of 0,.
It can be seen that when n is greater than or equal to 5 it can produce a suffix of 0, because 5*2=10. and 2 is certainly not missing because as long as there is an even number 2.
So we just need to count n!. Contains 5 of the number can be.
At first, I thought it would be as long as N/5. Later found, for example, 25, contains two 5,25=5*5. So it is found that the total number of 5 equals the number after N/5 divided by 5 until it is less than 5.
Public classSolution { Public intTrailingzeroes (intN) {if(n<4) return 0; intres=0; while(n>=5) {res+=n/5; N=n/5; } returnRes; }}
[Leetcode] Factorial Trailing Zeroes