Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
注意时间复杂度
public class Solution {
public int trailingZeroes(int n) {
int num=0;
int five = 1;
//注意时间复杂度需要小,如果num =0; five =5;while(five<=n){five*=5; num +=n/five;}就time out 了
while(five<=n/5){
five *=5;
num += n/five;
}
return num;
}
}
leetcode 172: Factorial Trailing Zeroes
原文:http://my.oschina.net/liusicong/blog/374698