题意
算出n!结尾有多少零。
我的思路
我的代码
这段代码是超时的,如果只是找五的倍数,其中很多次循环是没有必要的。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class Solution {
public int trailingZeroes(int n) {
int count = 0;
for(int i=n;i>=1;i--){
if(i%5==0){
int tmp = i;
while(tmp%5==0){
count++;
tmp = tmp/5;
}
}
}
return count;
}
}
我优化后的代码。
1 | public class Solution { |