public class Solution {
public int TrailingZeroes(int n) {
if (n == )
{
return ;
}
else
{
var x = n / ;
var y = TrailingZeroes(x);
return x + y;
}
}
}

https://leetcode.com/problems/factorial-trailing-zeroes/#/description

此类问题很显然属于数学问题,一定要找到其中的本质规律才能得到正确的数学模型。
两个大数字相乘,都可以拆分成多个质数相乘,而质数相乘结果尾数为0的,只可能是2*。如果想到了这一点,那么就可以进一步想到:两个数相乘尾数0的个数其实就是依赖于2和5因子的个数。又因为每两个连续数字就会有一个因子2,个数非常充足,所以此时只需要关心5因子的个数就行了。
对于一个正整数n来说,怎么计算n!中5因子的个数呢?我们可以把5的倍数都挑出来,即:
令n! = (*K) * (*(K-)) * (*(K-)) * ... * * A,其中A就是不含5因子的数相乘结果,n = *K + r(<= r <= )。假设f(n!)是计算阶乘n!尾数0的个数,而g(n!)是计算n!中5因子的个数,那么就会有如下公式:
f(n!) = g(n!) = g(^K * K! * A) = K + g(K!) = K + f(K!),其中K=n / (取整数)。
很显然,当0 <= n <= 4时,f(n!)=。结合这两个公式,就搞定了这个问题了。举几个例子来说: f(!) = + f(!) =
f(!) = + f(!) =
f(!) = + f(!) =
f(!) = + f(!) = + + f(!) =
f(!) = + f(!) = + + f(!) = + + f(!) = + + f() =

以上解释参考地址:https://www.cnblogs.com/kuliuheng/p/4102917.html

python的实现:

 class Solution:
def trailingZeroes(self, n: int) -> int:
count =
while (n > ):
count += n //
n = n //
return count

leetcode172的更多相关文章

  1. LeetCode----172. Factorial Trailing Zeroes(Java)

    package singlenumber136; //Given an array of integers, every element appears twice except for one. F ...

  2. 每天一道LeetCode--172. Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  3. [Swift]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  4. leetcode172 阶乘后的零

    对数算法:O(nlogn) /** 即为统计0-n中5,10,15,20,25的个数,因为肯定有足够的偶数使得存在x*5=10*n,25=5*5因此计数加2,5=1*5计数加一: 但如果挨个计数当n很 ...

  5. LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

    数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...

  6. LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)

    172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...

  7. leetcode探索中级算法

    leetcode探索中级答案汇总: https://leetcode-cn.com/explore/interview/card/top-interview-questions-medium/ 1)数 ...

随机推荐

  1. 快速使用CSS 弹性盒子

    布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现:2009年,W3C提出了一种新的方案 ...

  2. HIVE从路人到入门

    绪论 第一章 Hive的基本架构及原理 第二章 基础知识 第三章 基本操作 第四章 复杂操作 总结

  3. 用setTimeout实现setInterval函数

    最近get一个新知识,也不算是新知识,可能是以前自己没有认真对待(对自己无语ing,si不si傻). 废话不多说,直接来看代码吧 function setInterval(func, t){ var ...

  4. 005——php字符串中的处理函数(四)

    <?php /** * 字符串处理函数: * parse_url 解析URL.返回其组成部分 */ /* $url="http://www.lantianwang.com/admin/ ...

  5. javascript浅拷贝和深拷贝

    /* 浅拷贝 */ function extend(parent, child) { var i; child = child || {}; for (i in parent) { if (paren ...

  6. js enter键激发事件

    document.onkeydown = function (e) {            if (!e) e = window.event;            if ((e.keyCode | ...

  7. Git 之 git原理简介

    这里只是很简单.超简单的介绍下git,为的是方便记忆: 本地仓库分为三个部分:工作区.暂存区.仓库区,其中暂存区和仓库区属于版本区. 对于文件的操作,需要从工作区----> 暂存区 ----&g ...

  8. react 拖拽排序---原生

    定义css, 两个动画 .drag-up { -webkit-animation: dragup ease 0.2s 1; animation: dragup ease 0.2s 1; -webkit ...

  9. train validation test

    http://stats.stackexchange.com/questions/19048/what-is-the-difference-between-test-set-and-validatio ...

  10. Java移位运算符 “

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zjx409/article/details/37569055 左移运算符(<<) 基本使 ...