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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int trailingZeroes(int n) { | |
int ans=0; | |
while (n>=5) { | |
n/=5; | |
ans+=n; | |
} | |
return ans; | |
} | |
}; |
No comments:
Post a Comment