[LintCode] Trailing Zeroes 末尾零的个数

Write an algorithm which computes the number of trailing zeros in n factorial.

Have you met this question in a real interview?
Yes
Example

11! = 39916800, so the out should be 2

Challenge

O(log N) time

LeetCode上的原题,请参见我之前的博客Factorial Trailing Zeroes

解法一:

class Solution {
public:
// param n : description of n
// return: description of return
long long trailingZeros(long long n) {
long long res = ;
while (n > ) {
res += n / ;
n /= ;
}
return res;
}
};

解法二:

class Solution {
public:
// param n : description of n
// return: description of return
long long trailingZeros(long long n) {
return n == ? : n / + trailingZeros(n / );
}
};
上一篇:layui 读取本地excel内容_layui导入excel文件的方法是什么


下一篇:python全栈开发day33-进程间的通信、进程间的数据共享,进程池