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

Factorial Trailing Zeroes

2015-09-24 21:10 387 查看
【题目描述】

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.
【思路】
看n的阶乘里有多少个0,只要看1~n中有多少个2和5就可以了,又因为2肯定比5多,所以只要看5有多少个就可以了。另外,还要注意像25里包含了2个5,125里包含了3个5.。。

【代码】

class Solution {
public:
    int trailingZeroes(int n) {
        int ans=0;
        while(n){
            ans+=n/5;
            n=n/5;
        }
        return ans;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: