题目:

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. FileZilla ftp服务器安装

    在官网下载server版本的FileZilla,我下载的是(The latest stable version of FileZilla Server is 0.9.41) 然后直接安装就可了,我由于 ...

  2. 处理器在 protected mode 下的 protection

    前言 实模式,保护模式,分段,分页,虚拟内存,内核态,用户态,如果你对这些术语之间的关系非常熟悉,那就不用继续看了.这篇主要记录我对用户态/内核态的一些理解,如有不对还请指教. 下述说明均为 x86- ...

  3. 构造函数继承关键apply call

    主要我是要解决一下几个问题: 1.        apply和call的区别在哪里 2.        apply的其他巧妙用法(一般在什么情况下可以使用apply) 我首先从网上查到关于apply和 ...

  4. thymeleaf 内联语法

    十二. thymeleaf内联语法 内联:th:inline,值有三种:text,javascript,none 12.1 th:inline="text"文本内联 <p t ...

  5. ACE_linux:UDP通信

    1.涉及类 ACE_INET_Addr//ACE网络地址ACE_SOCK_Dgram//ACE报文 2.简介 UDP通信时无需像TCP那样建立连接和关闭连接,TCP编程时需要通过accept和conn ...

  6. [Oracle]Oracle学习小结(1)

    1.查看Oracle数据库中的所有用户: (1)使用具有DBA权限的账户登录数据库: (2)执行select username from dba_users. SQL> conn sys 输入口 ...

  7. spark向量

    转自 1.本地向量MLlib的本地向量主要分为两种,DenseVector和SparseVector,顾名思义,前者是用来保存稠密向量,后者是用来保存稀疏向量,其创建方式主要有一下三种(三种方式均创建 ...

  8. ubuntu下下载并安装H265(hm.x.x代码和X265代码)

    H265,现今是High Efficiency Video Coding的别称,详细的概述见维基百科,详细的开发见官方网站. 一.下载并编译官方的测试源码HM.x.x: 1 ubuntu下安装svn: ...

  9. 网络编程Socket UDP

    图表流程 linux udp测试代码 //server.c #include <stdio.h> #include <stdlib.h> #include <errno. ...

  10. .NET基础之深度复制和浅度复制

    之前一直没有搞清楚深度复制和浅度复制的区别到底在哪里,今天彻底把这个东西弄懂了,写出来与到家共勉. 如果大家不懂值类型和引用类型的区别,请先看http://www.cnblogs.com/Autumo ...