07/16/2016 Math

Question

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

Note: Your solution should be in logarithmic time complexity.

Solution

Result: Accepted Time: 4 ms

Here should be some explanations.

class Solution {
public:
int trailingZeroes(int n) {
int ans = 0;
while(n)
{
n/=5;
ans +=n;
}
return ans;
}
};


Complexity Analytics

• Time Complexity: $O(log(n))$
• Space Complexity: $O(1)$