q172_阶乘后的零

1

package 数字操作.q172_阶乘后的零.f1;
/**
 * 找因子直接遍历(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;
            }
        }
        return rs;
    }
}

2

package 数字操作.q172_阶乘后的零.f2;
/**
 * 基于方法一,寻找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;
    }
}