从零开始的LC刷题(42): Factorial Trailing Zeroes 阶乘0的数量

原题:

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

Example 1:

Input: 3
Output: 0
Explanation: 3! = 6, no trailing zero.

Example 2:

Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.

Note: Your solution should be in logarithmic time complexity.

数学题,没什么好讲的,很简单,结果:

Success

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Factorial Trailing Zeroes.

Memory Usage: 8.2 MB, less than 73.97% of C++ online submissions for Factorial Trailing Zeroes.

代码:

class Solution {
public:
    int trailingZeroes(int n) {
        int re=0;
        while(n!=0){
            n=n/5;
            re+=n;
        }
        return re;
    }
};

 

你可能感兴趣的:(LEETCODE,C++)