您的位置:首页 > 大数据 > 人工智能

172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0

2016-08-23 17:59 513 查看
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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: