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

Note: Your solution should be in logarithmic time complexity.

主要是思考清楚计算过程:

将一个数进行因式分解,含有几个5就可以得出几个0(与偶数相乘)。

代码很简单。

  1. public class Solution {
  2. public int trailingZeroes(int n) {
  3. int result = 0;
  4. long num = 5;
  5. long div = (long) n;
  6. while (div / num != 0){
  7. result += div / num;
  8. num *= 5;
  9. }
  10. return result;
  11. }
  12. }

✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java的更多相关文章

  1. [LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数

    所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125.. ...

  2. leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)

    数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...

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

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

  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 (阶乘末尾零的数量)

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

  6. Java [Leetcode 172]Factorial Trailing Zeroes

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

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

  8. 172 Factorial Trailing Zeroes 阶乘后的零

    给定一个整数 n,返回 n! 结果尾数中零的数量.注意: 你的解决方案应为对数时间复杂度. 详见:https://leetcode.com/problems/factorial-trailing-ze ...

  9. Leetcode 172 Factorial Trailing Zeroes

    给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...

随机推荐

  1. Git版本库

    创建版本库:git init db 只要用git init db 就可以很容易创建一个空的Git版本库. Git版本库创建好之后,在版本库的目录下有一个.git的子目录中有几项内容,其中注意三项: 1 ...

  2. php 错误堆栈

    ob_start();  debug_print_backtrace();   $errpr = ob_get_clean();

  3. Intellij IDEA 创建Web项目并在Tomcat中部署运行(不使用maven)【转载】

    原文链接:http://www.thinksaas.cn/topics/0/350/350000.html 一.创建Web项目 1.File -> New Module,进入创建项目窗口 2.选 ...

  4. 【转帖】Python在大数据分析及机器学习中的兵器谱

    Flask:Python系的轻量级Web框架. 1. 网页爬虫工具集 Scrapy 推荐大牛pluskid早年的一篇文章:<Scrapy 轻松定制网络爬虫> Beautiful Soup ...

  5. android base64 和 aes 加密 解密

    package pioneerbarcode.ccw.com.encryptanddecode;import android.os.Bundle;import android.support.v7.a ...

  6. google closure继承模块三:goog.base()源码分析

    直接看代码吧: base: function (me, opt_methodName, var_args) { var caller = arguments.callee.caller; if (ca ...

  7. USB协议规范学习(一)

    什么是USB OHCI规范? OHCI(Open HCI)是目前使用比较广泛的三种USB主机控制器规范之一.USB体系结构是由四个主要部分组成:客户软件/USB驱动,主机控制器驱动(HCD),主机控制 ...

  8. Win7下Hyenae的安装

    (1)下载 链接:http://sourceforge.net/projects/hyenae/   资源:hyenae-0.36-1_fe_0.1-1-win32.exe (2)README --- ...

  9. IOC和Aop使用的扩展

    下面还有静态代理和动态代理 1.构造注入 lib包: 在entity包下新建一个实体类User 代码: package cn.happy.entity; public class User { pri ...

  10. 互联网实习笔记之shell笔记

    linux下面一切都是可以配置的 #vim可以有 .vimrc文件 #------.vimrc开始---- set vb t_vb= set number syntax on set hlsearch ...