【LeetCode】172. Factorial Trailing Zeroes 解题小结

题目:

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

Note: Your solution should be in logarithmic time complexity.

这题主要是计算n!中有多少个5存在,有5的话必定会有0。首先除以5,然后再看因子中有多少个25,然后是125,依次类推。

class Solution {
public:
    int trailingZeroes(int n) {
        int zeros = 0;
        int curr = 0;
        int i = 1;
        while( (curr = n / pow(5,i)) >= 1){
            zeros += curr;
            i++;
        }
        
        return zeros;
    }
};

 

posted on 2016-09-25 22:58  医生工程师  阅读(153)  评论(0编辑  收藏  举报

导航