您的位置:首页 > 大数据 > 人工智能

[LeetCode] Factorial Trailing Zeroes

2015-07-13 20:25 549 查看
Question:

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

1、题型分类:

2、思路:寻找n!后面的0的个数,即有多少个2*5,从而需要寻找里面总共有多少个2和多少个5,2肯定比5多,则只要找出5的个数即可。n/5是从n/5到n中5的倍数的个数

3、时间复杂度:

4、代码:

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


5、优化:

6、扩展:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: