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

Note: Your solution should be in logarithmic time complexity.

求n得阶层,末尾0的个数

老问题了,统计2,5的个数,明显2的个数要多。

只要统计5的个数就可以了,5的个数 = sum(n/5^1,  n/5^2, n/5^3...) 也就是 1~n中包含多少个5,25,125,5^x 

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

 

相关文章:

  • 2021-04-02
  • 2021-11-15
  • 2021-06-30
  • 2022-12-23
  • 2021-08-09
  • 2021-08-18
猜你喜欢
  • 2022-01-23
  • 2021-08-22
  • 2021-06-03
  • 2022-02-23
  • 2021-12-12
  • 2022-12-23
相关资源
相似解决方案