给定一个整数 n,返回 n! 结果尾数中零的数量。
注意: 你的解决方案应为对数时间复杂度。

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

Java实现:

N的阶乘可以分解为: 2的X次方,3的Y次方,4的K次方,5次Z方,.....的乘积。由于10 = 2 * 5,所以M只能和X和Z有关,每一对2和5相乘就可以得到一个10,于是M = MIN(X,Z),不难看出X大于Z,因为被2整除的频率比被5整除的频率高的多。所以可以把公式简化为M=Z。

方法一:

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

方法二:超时

class Solution {
public int trailingZeroes(int n) {
int res=0;
for(int i=5;i<=n;++i){
int m=i;
while(m%5==0){
++res;
m/=5;
}
}
return res;
}
}

参考:https://www.cnblogs.com/grandyang/p/4219878.html

172 Factorial Trailing Zeroes 阶乘后的零的更多相关文章

  1. ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java

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

  2. leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)

    数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...

  3. [LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数

    所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125.. ...

  4. 【LeetCode】172. Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

  5. [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数

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

  6. LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)

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

  7. Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

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

  8. 172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0

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

  9. 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)

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

随机推荐

  1. android之检測是否有网络

    主要是用来检測是否有网络,假设没有,就去wifi里面去进行设置网络... 以下贴一下主要代码: private void checkNetWorkInfo() { if (!Tools.isNetwo ...

  2. Android开发文档翻译之-Services

    Service是一种能长期在后台运行同一时候不须要与用户进行交互的应用组件.其它组件能够开启service,开启后service能够自行运行及时用户已经切换到其它的应用.此外,组件能够与service ...

  3. 2 AngularJS 1 概念浓缩

    Angular Web APP 结构图: module   --> 模块     :相当于一个容器,Angular里的所有东西都得放在模块里,才能够被引用和加载. directive  --&g ...

  4. PS人物脸部去高光简单之法

    案例素材图: 方法原理步骤:得到高光面的选区,然后吸取高光面附近的颜色填充上去,这样就达到了去高光的效果. 得到高光选区的方法有很多种,要提取这种选区,通过阿尔法通道是最合适不过的了,本案例就通过阿尔 ...

  5. XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】

    1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 45  Solved: 8[Submit][Status][W ...

  6. adbi命令【转】

    本文转载自:https://zmywly8866.github.io/2015/01/24/all-adb-command.html   ADB很强大,记住一些ADB命令有助于提高工作效率. 获取序列 ...

  7. POSTMAN模拟数组数据

    有时候写接口,需要传入数据数据.比如购物车中的一组商品.它们的数量是不固定的,只能用数组才能更好的处理. 怎么用POSTMAN模拟呢? 万能的POSTMAN.

  8. PHPExcel使用收藏

    注意:PHP7版本中phpexcel导出文件是提示找不到文件,需修改PHPExcel目录下的calculation目录下的Functions.php的581行  去掉break; 下面是总结的几个使用 ...

  9. 数据库sqlite3的使用-基本语法

    一.SQL语句 如果要在程序运行过程中操作数据库中的数据,那得先学会使用SQL语句 1.什么是SQL SQL(structured query language):结构化查询语言 SQL是一种对关系型 ...

  10. 获取Android APK JNI库

    /************************************************************************** * 获取Android APK JNI库 * 说 ...