[LintCode] Trailing Zeroes 末尾零的个数

简介:

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

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

解法二:

class Solution {
 public:
    // param n : description of n
    // return: description of return 
    long long trailingZeros(long long n) {
        return n == 0 ? 0 : n / 5 + trailingZeros(n / 5);
    }
};

本文转自博客园Grandyang的博客,原文链接:末尾零的个数[LintCode] Trailing Zeroes ,如需转载请自行联系原博主。

相关文章
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
Biggest Number深搜
You can start from any square, walk in the maze, and finally stop at some square. Each step, you may only walk into one of the four neighbouring squares (up, down, left, right) and you cannot walk into obstacles or walk into a square more than once.
120 0
Biggest Number深搜
|
机器学习/深度学习 人工智能