LeetCode 172:Factorial Trailing Zeroes

来源:互联网 发布:ab模板源码 百度网盘 编辑:程序博客网 时间:2024/06/16 05:51

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

Note: Your solution should be in logarithmic time complexity.

给定一个整数n,返回n!(n的阶乘)末尾的0的个数。

注意:你的函数应为对数的时间复杂度


因为只有2X5=10,而2的个数远比5多,因此找5的个数就可以了。。。

class Solution {public:    int trailingZeroes(int n) {        if(n==0||n==1) return 0;        int count=0;        if(n%10==0) count++;        return count+trailingZeroes(n-1);    }};






0 0
原创粉丝点击