Factorial Trailing Zeroes
Total Accepted: 44612 Total Submissions: 144778 Difficulty: Easy
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
n!的结果中0的个数。
0是由5与偶数相乘产生的,所以我们只需计算0-n中有多个“5”,注意25包含两个5 , 125包含3个5。
所以: cnt = n/5+n/25+n/125+...
class Solution { public: int trailingZeroes(int n) { int res = 0; while(n){ res += n/5; n /= 5; } return res; } };
时间: 2024-12-28 15:48:54