Tuesday, March 29, 2016

LeetCode Q172: Factorial Trailing Zeros

Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.

Solution:
Zeros is produced by "5 x even number", so count how many 5s in each number.


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

No comments:

Post a Comment