原题链接在这里: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. mongodb与mysql命令对比

    mongodb与mysql命令对比 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(col ...

  2. ARC指南3 - @property

      本章介绍引入ARC后@property的使用,跟ARC之前的还是很不一样的 一..h和.m文件的变化说明 1.对于.h头文件,主要是将属性定义由retain变为strong @property ( ...

  3. 【液晶模块系列基础视频】3.4fatfs接口函数的使用4

    ============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...

  4. UITableview cell中多个按钮

    xib的 //不使用这种- (IBAction)button:(UIButton *)sender; //使用这种 @property (weak, nonatomic) IBOutlet UIBut ...

  5. ajax中文传送到模板显示为null

    问题: 名称空间声明语句必须是第一个语句中的脚本: Fatal error: Namespace declaration statement has to be the very first stat ...

  6. 使用FROM确认按钮(键盘13号键)提交特性并使用ajax.POST提交.

    如果又想使用FROM确认按钮(键盘13号键)提交特性  还能继续用AJAX.POST提交.就需要使用return false 来阻止FROM默认提交 代码如下: HTML页面 这里最关键就是用了ret ...

  7. Bootstrap页面布局24 - BS旋转木马功能

    代码: <div class='container-fluid'> <h3 class='page-header'>Bootstrap 旋转木马</h3> < ...

  8. spotlight监控工具使用

    利用spotlight工具可以监控如下系统:        1.Spotlight on Unix 监控Linux服务器 1)安装 Spotlight on Unix 2)配置spotlight登陆用 ...

  9. 【转载】C内存分配

    一.预备知识—程序的内存分配  一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. ...

  10. linux 自动登录脚本

    #!/usr/bin/expect set port 22 set user xiaoming set password xiaoming123 set host 111.222.22.33 set ...