leetcode--Factorial Trailing Zeroes

来源:互联网 发布:docker daemon 端口 编辑:程序博客网 时间:2024/06/14 00:02

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

Note: Your solution should be in logarithmic time complexity.

[java] view plain copy
  1. public class Solution {  
  2.     /** 
  3.      * 本质就是计算因子中5的个数 
  4.      * n/5可以求出1-n有多少个数能被5整除 
  5.      * 接着求1-n/5有多少个数能被5整除,也就是能整除25,以此类推 
  6.      * @param n 
  7.      * @return 
  8.      */  
  9.     public int trailingZeroes(int n) {                
  10.          int counter  = 0;  
  11.          while(n > 1){  
  12.              counter += n / 5;//计算1-n有多少个数能被5整除  
  13.              n = n / 5;//计算5^n  
  14.          }    
  15.          return counter;  
  16.     }  
  17. }  


原文链接http://blog.csdn.net/crazy__chen/article/details/45441421

原创粉丝点击