LeetCode题解:Factorial Trailing Zeroes

来源:互联网 发布:德军总部2 知乎 编辑:程序博客网 时间:2024/05/16 11:11

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

题意:求n阶乘结果中有多少0

解决思路:阶乘要出现0必然能分解为5*2,2的数量大于5的数量,所以每一个5必然有一个2匹配,所以计算5的个数即可

代码:

public class Solution {    public int trailingZeroes(int n) {        int count = 0;        while(n > 0){            n /= 5;            count += n;        }        return count;    }}
0 0
原创粉丝点击