leetcode--Factorial Trailing Zeroes

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;
    }
}


你可能感兴趣的:(leetcode--Factorial Trailing Zeroes)