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

Note: Your solution should be in logarithmic time complexity.

 

只有2 * 5才会产生0只要计算出因子中2和5的个数取小的的那个就好了

 1 public class Solution {
 2     public int trailingZeroes(int n) {
 3         int numsOf2 = 0;
 4         int numsOf5 = 0;
 5         for(int i = 2; i <= n; i++){
 6             numsOf2 += get2nums(i);
 7             numsOf5 += get5nums(i);
 8         }
 9         return numsOf2 < numsOf5 ? numsOf2 : numsOf5;
10     }
11     
12     /**
13      * 计算num中2作为乘积因子的个数
14      * @param num
15      * @return
16      */
17     private int get2nums(int num){
18         int numsOf2 = 0;
19         if(num % 2 != 0)
20             return 0;
21         else{
22             while(num % 2 == 0){
23                 num = num / 2;
24                 numsOf2 ++;
25             }
26         }
27         return numsOf2;
28     }
29     
30     /**
31      * 获取5的个数
32      * @param num
33      * @return
34      */
35     private int get5nums(int num){
36         int numsOf5 = 0;
37         if(num % 5 != 0)
38             return 0;
39         else{
40             while(num % 5 == 0){
41                 num = num / 5;
42                 numsOf5 ++;
43             }
44         }
45         return numsOf5;
46     }
47 }