您的位置:首页 > 大数据 > 人工智能

Factorial Trailing Zeroes 阶乘的后边有几个0

2015-12-30 18:07 441 查看
Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

//所有的因子里边含有2和5的个数,而2的个数明显会多余5的,所以只需要计算含有因子5为几个就是几个0,像25*4=100,所以25含有2个5(10含有一个2和一个5,)



public class Solution {

    public int trailingZeroes(int n) {

        int sum=0;

        while(n>0){

            sum+=n/5;

            n=n/5;

        }

        return sum;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: