[leetcode] Factorial Trailing Zeroes

Factorial Trailing Zeroes

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 res = 0;
        while(n) {
            n /= 5;
            res += n; 
        }
        return res;
    }
};
View Code

 

posted on 2015-01-15 15:22  cha1992  阅读(98)  评论(0编辑  收藏  举报

导航