原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/

题目:

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

Example 1:

Input: 3
Output: 0
Explanation: 3! = 6, no trailing zero.

Example 2:

Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.

Note: Your solution should be in logarithmic time complexity.

题解:

求factorial后结尾有多少个0, 就是求有多少个2和5的配对.

但是2比5多了很多,所以就是求5得个数。除此之外,还有一件事情要考虑。诸如25, 125之类的数字有不止一个5. e.g. n = 28, n!我们得到一个额外的5, 并且0的总数变成了6.

0 factorial 是 1. 不用单独考虑n == 0的情况.

n!后缀0的个数 = n!质因子中5的个数

             = floor(n/5) + floor(n/25) + floor(n/125) + ....

Time Complexity: O(logn). Space: O(1).

AC Java:

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

LeetCode Factorial Trailing Zeroes的更多相关文章

  1. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

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

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

  3. 关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解

    题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0 ...

  4. [LeetCode] Factorial Trailing Zeroes 阶乘末尾0

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

  5. Python3解leetcode Factorial Trailing Zeroes

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

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

    题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...

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

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

  8. 【LeetCode】172. Factorial Trailing Zeroes

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

  9. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

随机推荐

  1. 封装同步的UIActionSheet

    封装同步的UIActionSheet 发问题 做 iOS 开发的同学想必都用过 UIActionSheet.UIActionSheet 可以弹出一个选择列表,让用户选择列表中的某一项操作.使用 UIA ...

  2. lightning mdb 源代码分析(5)-事务控制

    本博文系列前面已经探讨了LMDB的系统架构.MMAP映射.B-Tree操作等部分,本文将尝试描述LMDB中的事务控制的实现. 事务的基本特征: 事务是恢复和并发控制的基本单位.它是一个操作序列,这些操 ...

  3. PowerCmd-cmd命令行管理工具

    缘由 看了大漠在慕课网关于angular的讲解,用到了PowerCmd.就百度一下准备下载,很小很实用的工具,发现有人总结了.备忘 http://www.cnblogs.com/xing901022/ ...

  4. oracle imp导入库到指定表空间

    1.创建表空间 create tablespace example_tablespace datafile 'e:\****.dbf' size 10m reuse autoextend on nex ...

  5. jQuery如何去判断页面是否有父页面?

    jQuery如何去判断页面是否有父页面?     是要判断当前页面是否被嵌入在frame里吗? 1 2 3 if (top != self) {     alert('我在框架里'); }

  6. 18. 求交错序列前N项和

    求交错序列前N项和 #include <stdio.h> int main() { int numerator, denominator, flag, i, n; double item, ...

  7. skype msnLite 静态路由

    连接vpn后防止skype和msn重复登陆 route -p add ip网段 mask 子网掩码 本地网管 skyperoute -p add 91.0.0.0 mask 255.0.0.0 192 ...

  8. 20145235 《Java程序设计》第6周学习总结

    教材学习内容总结 10.1 InputStream与OutputStream 串流设计的概念 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 从应用程序角度来看,如果要将 ...

  9. CLI下另一种多进程实现方式----PCNTL

    有些时候,你需要对一些脚本进行优化,以期跑的更快,在更短的时间内完成任务.PCNTL是一个不错的选择,它可以fork多个进程,来协同完成一个任务,理论上完成的时间将会和进程数成反比. 不过,PCNTL ...

  10. java实现的https请求

    转载并修改自 http://www.blogjava.net/etlan/archive/2006/06/29/55767.html Https请求 超文本传输协议HTTP协议:被用于在Web浏览器和 ...