[leetcode] 学习记录——Factorial Trailing Zeroes

来源:互联网 发布:js中获取json对象的值 编辑:程序博客网 时间:2024/06/15 10:58

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

Note: Your solution should be in logarithmic time complexity.


分析:这是一道求n!后面有几个0的问题,不要用算法的角度去考虑,因为考虑到测试用例肯定会有大数,因此int得到结果后在数0是不可取的,并且算法的时间远远会超过log(n),  可以发现规律,0的数量与5有关。比如:5!有一个0 10!有两个0,问题迎刃而解

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


0 0