题目:

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

Note: Your solution should be in logarithmic time complexity.

链接: http://leetcode.com/problems/factorial-trailing-zeroes/

题解:

求n!里有多少个0。其实主要就是看有多少个5,有多少个5就有多少个0,这样我们就可以用一个while循环来搞定。

Time Complexity - O(logn), Space Complexity - O(1)

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

二刷:

找到在n里有多少个5,就可以得到结果。

Java:

Time Complexity - O(logn), Space Complexity - O(1)

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

三刷:

题目可以转化为,求n!中含有多少个5。如何计算一个数里面有多少个5呢?我们可以用以下公式:

count = n / 5 + n / 25 + n / 125 + ....

就是用n除5来取得一开始的基数,当遇到5的倍数的时候,我们也要作相应的增加, 转换为循环的话我们可以先计算单个5的个数  n / 5,然后 n /= 5来计算 25的个数,然后再继续。最后返回count.

Java:

Time Complexity - O(logn), Space Complexity - O(1)

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

Reference:

https://leetcode.com/discuss/62976/my-python-solution-in-o-1-space-o-logn-time

https://leetcode.com/discuss/19847/simple-c-c-solution-with-detailed-explaination

http://15838341661-139-com.iteye.com/blog/1883889

172. Factorial Trailing Zeroes的更多相关文章

  1. 【LeetCode】172. Factorial Trailing Zeroes

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

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

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

  3. 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 ...

  4. ✡ 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 ...

  5. Java for LeetCode 172 Factorial Trailing Zeroes

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

  6. 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 ...

  7. Java [Leetcode 172]Factorial Trailing Zeroes

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

  8. 【一天一道LeetCode】#172. Factorial Trailing Zeroes

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  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. DEDECMS中,获取面包屑导航

    获取面包屑导航 {dede:field name='position'/} {dede:field.position/}

  2. ubuntu修改登录信息(本机和SSH登录)

    1.需要修改的主要文件和目录如下:/etc/issue/etc/motd/etc/update-motd.d//etc/issue.net 2.Ubuntu的登陆和欢迎信息控制主要在/etc/issu ...

  3. sphinx 占用大量内存

    http://www.coreseek.com/forum/2_1847_0.html(转) 刚开始没改下面2句时,内存占用比较多,在生成index的时候就占用了! 下面是解决方法: 每个索引中写上 ...

  4. 多项分布(multinominal distribution)

    简介 更一般性的问题会问:“点数1~6的出现次数分别为(x1,x2,x3,x4,x5,x6)时的概率是多少?其中sum(x1~x6)= n”.这就是一个多项式分布. 定义 把二项分布推广至多个(大于2 ...

  5. Java中的类加载器以及Tomcat的类加载机制

    在加载阶段,虚拟机需要完成以下三件事情: 1.通过一个类的全限定名来获取其定义的二进制字节流. 2.将这个字节流所代表的静态存储结构转化为方法区的运行时数据结构. 3.在Java堆中生成一个代表这个类 ...

  6. iOS Mac系统下Ruby环境安装

    由EasyIOS引出的一系列问题:转载的上一篇CocoaPods安装和使用教程中说明了,为什么要使用cocoapods ,但是要安装cocoapods需要Ruby环境,安装Ruby环境首先需要安装Xc ...

  7. PHP将二进制文件存入数据库以及从数据库中读取二进制文件

    <?php $file = 'abcd.sqlite'; mysql_connect('localhost','root','123456'); mysql_select_db('zblog') ...

  8. 51nod1212无向图最小生成树

    1212 无向图最小生成树 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树.   Inpu ...

  9. APACHE支持.htaccess以及 No input file specified解决方案

    在你的Apache安装文件夹conf里找到httpd.conf文件 搜索LoadModule rewrite_module modules/mod_rewrite.so 如果前面有注释符号#,请去掉. ...

  10. Linux编程学习笔记 -- Process

    进程是一个程序的运行.   在一个程序中执行另一个执程序的方法有两种: 1)system 在shell中执行程序 2)fork + exec 复制一个进程,在进程中用新的程序替换原有的程序   for ...