leetcode--Factorial Trailing Zeroes

来源:互联网 发布:零售业的数据分析 编辑:程序博客网 时间:2024/05/22 12:31

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

Note: Your solution should be in logarithmic time complexity.

public class Solution {    /** * 本质就是计算因子中5的个数 * n/5可以求出1-n有多少个数能被5整除 * 接着求1-n/5有多少个数能被5整除,也就是能整除25,以此类推 * @param n * @return */public int trailingZeroes(int n) { int counter  = 0; while(n > 1){ counter += n / 5;//计算1-n有多少个数能被5整除 n = n / 5;//计算5^n } return counter;    }}


0 0