加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Leetcode 172 Factorial Trailing Zeroes

发布时间:2020-12-13 21:19:49 所属栏目:PHP教程 来源:网络整理
导读:Given an integer n ,return the number of trailing zeroes in n !. Note: Your solution should be in logarithmic time complexity. 求阶乘的后缀0个数 乘法中的零来源于10,10来源于2和5,在阶乘中,1个数的质因子出现1次5,那末必定有其他数的质因子出

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

Note: Your solution should be in logarithmic time complexity.

求阶乘的后缀0个数

乘法中的零来源于10,10来源于2和5,在阶乘中,1个数的质因子出现1次5,那末必定有其他数的质因子出现若干次2

所以问题变成求解质因子5出现的次数,

n/5求出包括1个5的数字个数

n/25求出包括两个5的数字个数...以此类推

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


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读