作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目描述

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.

题目大意

递归

分析一下,就是看n!有多少个5组成。
计算包含的2和5组成的pair的个数就可以了。
因为5的个数比2少,所以2和5组成的pair的个数由5的个数决定。
观察15! = 有3个5(来自其中的5, 10, 15), 所以计算n/5就可以。
但是25! = 有6个5(有5个5来自其中的5, 10, 15, 20, 25, 另外还有1个5来自25=(5*5)的另外一个5),
所以除了计算n/5, 还要计算n/5/5, n/5/5/5, n/5/5/5/5, …, n/5/5/5,/5直到商为0。

最后的结果就是

return n/5 + n/25 + n/125 + n/625 + n/3125+...;

代码如下:

class Solution(object):
def trailingZeroes(self, n):
"""
:type n: int
:rtype: int
"""
res = 0
i = 5
while n >= i:
res += n / i
i *= 5
return res

循环

这个题一看就不能暴力求解。

算法中这个count += n / i;的意思是直接看有多少个5.
比如n=26;那么,26/5=5;26/25=1;所以结果是5+1=6个。

另外注意,一定用long。

public class Solution {
public int trailingZeroes(int n) {
if(n<=0) return 0;
int count = 0;
for (long i = 5; n / i >= 1; i *= 5) {
count += n / i;
}
return count;
}
}

AC:1ms

python解法如下:

class Solution(object):
def trailingZeroes(self, n):
"""
:type n: int
:rtype: int
"""
res = 0
i = 5
while n >= i:
res += n / i
i *= 5
return res

日期

2016 年 5 月 8日
2018 年 11 月 24 日 —— 周六快乐

【LeetCode】172. Factorial Trailing Zeroes 解题报告(Java & Python)的更多相关文章

  1. 【LeetCode】283. Move Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

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

  3. Java [Leetcode 172]Factorial Trailing Zeroes

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

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

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

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

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

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

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

  8. 【原创】leetCodeOj --- Factorial Trailing Zeroes 解题报告

    原题地址: https://oj.leetcode.com/problems/factorial-trailing-zeroes/ 题目内容: Given an integer n, return t ...

  9. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

随机推荐

  1. acquire

    An acquired taste is an appreciation for something unlikely to be enjoyed by a person who has not ha ...

  2. tomcat在eclipse上发布,Perference下的server找不到解决办法

    help--->Install New software得到如下所示 下面work with选项的内容与你的eclipse版本有关 我的eclipse版本为eclipse-java-2019-0 ...

  3. JavaScript的数据结构快速学-链表的实现

    1-单项链表 function LinkedList() { let Node = function(element) { // 辅助类,表示要添加到链表中的项 this.element = elem ...

  4. win10产品密钥 win10永久激活密钥(可激活win10所有版本)

    https://www.win7w.com/win10jihuo/18178.html#download 很多人都在找2019最新win10永久激活码,其实win10激活码不管版本新旧都是通用的,也就 ...

  5. GO 总章

    GO 学习资源 go 代理 GO 语言结构 GO 数字运算 GO 时间处理 GO 定时器 GO 异常处理 go recover让崩溃的程序继续执行 GO Exit Fatal panic GO 通过进 ...

  6. 如何在linux 上配置NTP 时间同步?

    故障现象: 有些应用场景,对时间同步的要求严格,需要用到NTP同步,如何在linux上配置NTP时间同步? 解决方案: 在linux 上配置NTP 时间同步,具休操作步骤,整理如下: 1.安装软件包( ...

  7. GO类型转换

    golang []byte转string golang中,字符切片[]byte转换成string最简单的方式是 package main import ( "fmt" _ &quo ...

  8. “==” 和 equals()的区别

    ※ "==" 和 equals()的区别 ※ == :比较. 基本数据类型比较的是值:. 引用类型比较的是地址值. ※ equals(Object o):1)不能比较基本数据类型, ...

  9. TCP协议三步挥手与四步挥手

    关于TCP协议 TCP(Transmission Control Protocol, 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议.与之对应的是UDP(User Datagram ...

  10. 13.Vue.js 组件

    组件(Component)是 Vue.js 最强大的功能之一. 组件可以扩展 HTML 元素,封装可重用的代码. 组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的界面都可以抽 ...