leetcode 172: Factorial Trailing Zeroes

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

Note: Your solution should be in logarithmic time complexity.this

Credits:
Special thanks to @ts  for adding this problem and creating all test cases.spa

注意时间复杂度.net

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;
    }
}
相关文章
相关标签/搜索