Factorial Trailing Zeroes

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;
    }
};

 

posted @ 2015-12-18 18:38  zengzy  阅读(128)  评论(0编辑  收藏  举报
levels of contents