2015年10月24日星期六

Leetcode 172 Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
Solution 1:  10=2*5, there is enough 2, just need to find how many "5" in the range of 1...n.
 public class Solution {  
   public int trailingZeroes(int n) {  
     int res=0;  
     for (int div=5; div<=n; div*=5) {  
       res+=n/div;  
       if (div>Integer.MAX_VALUE/5) break;  
     }  
     return res;  
   }  
 }  

没有评论:

发表评论