Leetcode 172. Factorial Trailing Zeroes (Easy) (cpp)
Tag: Math
Difficulty: Easy
/*
172. Factorial Trailing Zeroes (Easy)
Given an integer n, return the number of trailing zeroes in n!.'/[/'
Note: Your solution should be in logarithmic time complexity.
*/
class Solution {
public:
int trailingZeroes(int n) {
int res = 0;
while (n > 0) {
n /= 5;
res += n;
}
return res;
}
};

本文介绍LeetCode上第172题Factorial Trailing Zeroes的解题思路及实现方法,采用数学方法计算阶乘尾部零的数量,并确保解决方案的时间复杂度为对数级。

561

被折叠的 条评论
为什么被折叠?



