leetcode--Factorial Trailing Zeroes

来源:互联网 发布:lv男士名片夹 淘宝 编辑:程序博客网 时间:2024/06/13 22:30

题目:Factorial Trailing Zeroes

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

Note: Your solution should be in logarithmic time complexity.

计算n的阶乘的拖尾0的个数,即n中5的倍数的个数。

One:

public class Solution {    public int trailingZeroes(int n) {        if(n<0){            return 0;        }        int c = 0;        while(n/5!=0){            n/=5;            c+=n;        }        return c;    }}
Two:递归

public int trailingZeroes(int n) {    return n>=5 ? n/5 + trailingZeroes(n/5): 0;}



0 0
原创粉丝点击