172. Factorial Trailing Zeroes

2018/04/21 leetcode 共 466 字,约 2 分钟

Title

the problem link

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

Note: Your solution should be in logarithmic time complexity.

Credits: Special thanks to @ts for adding this problem and creating all test cases.

题目大意:到现在也不是很清楚,大概就是输入n,计算n!中有多少个0,看solution ans discuss意思是计算有多少个5,因为2肯定比5多,然后25=10,就有0了,计算有多少个5就直接n/5个了,但要知道25可以分解成55,所以要加上n/25,然后5^3,5^4….

Solution

//cpp:
class Solution {
public:
    int trailingZeroes(int n) {
        long long result=0;
        for(long long i=5;n>=i;i*=5){
            result+=n/i;
        }
        return result;
    }
};

Search

    Table of Contents