Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
(1)
class Solution { public: int trailingZeroes(int n) { int ans = 0; for(long long i = 5; n / i; i *= 5) { ans += n / i; } return ans; } };
(2)
class Solution { public: int trailingZeroes(int n) { int ans = 0; while(n) { int t = n / 5; ans += t; n = t; } return ans; } };
时间: 2024-10-10 19:54:47