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

leetcode:Factorial Trailing Zeroes

2015-07-15 22:46 441 查看

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 cnt = 0;
/*long long i = 5;//之前一直TLE 是因为把i的类型定义成整型,这样当n为整型最大值时,i乘以5将会超出整型范围变成负数,这样n除以i将不会为0而陷入死                          //循环,后来发现其实i的定义纯属多余,以后一定要注意细节
while(n / i)
{
cnt += n / i;
i *= 5;
}*/
while(n)
{
cnt += n / 5;
n /= 5;
}
return cnt;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: