p172

LeetCode p172 Factorial Trailing Zeroes 题解

1.题目:

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

Note: Your solution should be in logarithmic time complexity.

题意:

输入n输出n的阶乘的末尾有多少个0.

2.解题思路:

10=2×5
由于2比5多。所以统计因子中所有的5.

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
 
public class Solution {
public int trailingZeroes(int n) {
int ans = 0;
while (n > 4) {
ans = ans + n / 5;
n = n / 5;
}
return ans;
}
}

4.一些总结: