数字操作 —— 172_阶乘后的零

5. 172_阶乘后的零
/*
给定一个整数 n,返回 n! 结果尾数中零的数量。
*/

/**
 * 算一下乘法因子里有多少个5
 * 找因子直接遍历(o(n)超时)
 */
public class Solution {
    public int trailingZeroes(int num) {
        int rs = 0;
        for (int i = 1; i <= num; i++) {
            int j = i;
            while (j % 5 == 0) {
                rs++;
                j /= 5;
                if(j == 0) break;
            }
        }
        return rs;
    }
}
/**
 * 基于方法一,寻找5出现的规律o(log(n))
 */
public class Solution {
    public int trailingZeroes(int n) {
        int count = 0;
        while (n > 0) {
            count += n / 5;
            n = n / 5;
        }
        return count;
    }
}
原文地址:https://www.cnblogs.com/s841844054/p/13736450.html