leetcode解题之172# Factorial Trailing Zeroes Java版 (求n的阶乘末尾0的个数)


172. Factorial Trailing Zeroes

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

Note: Your solution should be in logarithmic time complexity.

给定一个整数n,求阶乘结果的尾部0的个数。

       假如i可以被5整除,则可以提供的5的个数为i/5个
  假如i可以被25整除,则可以多提供的5的个数为i/25个
  假如i可以被125整除,则可以多提供的5的个数为i/125个(算上了被5,25整除之后)

  public int trailingZeroes(int n) {
int res = 0;
while(n>0)
{
res += n/5;
n /= 5;
}
return res;
}






注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号