本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42417535

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

Note: Your solution should be in logarithmic time complexity.

思路:

(1)题意为求解一个整数经过阶乘计算得到结果中有多少个0。

(2)我们知道0的个数和2和5有关,而2的个数要远远多于5的个数,所以求得5的个数即为0的个数。

(3)但是对于25、125这样由若干个5相乘组合的,需要计算待求解整数中有多少个这样的数。

(4)例如对于1000来说,1000/5=20,100/25=40,1000/125=8,1000/625=1,1000/1250=0,即0的个数为20+40+8+1=69个。

(5)希望本文对你有所帮助。

算法代码实现如下所示:

public static  int trailingZeroes(int n) {
	int count=0;
	while(n>0){
		count = count + n/5;
		n=n/5;
	}
	return count;
}

Leetcode_172_Factorial Trailing Zeroes的更多相关文章

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

  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. [LintCode] Trailing Zeroes 末尾零的个数

    Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...

  4. 【leetcode】Factorial Trailing Zeroes

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

  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】Factorial Trailing Zeroes(easy)

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

  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. leetcode:Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. 最初的代码 class Solution { public: int t ...

  9. LeetCode Factorial Trailing Zeroes

    原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...

随机推荐

  1. ACM Wooden Stricks

    有一堆n根木棍.每根棍子的长度和重量是预先知道的. 这些棒子将被木工机器逐一加工..它需要一些时间,称为安装时间,用于机器准备加工棒.设置时间与机器中的清洁操作和更换工具和形状相关联.木工机械的安装时 ...

  2. 使用Java可以做得一些事

    安卓 Web JSP使用Echarts的最简单的例子 微信 wechat4j weixin-java-tools weixin4j 网络服务器

  3. Android文件大头10G

    这个玩意直接10G....记录下. C:\Users\xn\AppData\Local\Android\sdk\system-images\android-23

  4. 给大家安利一个学习angular2的视频网站

    本文地址:http://blog.csdn.net/sushengmiyan 本文作者:苏生米沿 视频地址: https://egghead.io/courses/angular-2-fundamen ...

  5. windows10,redhat6.5下python3.5.2使用cx_Oracle链接oracle

    0.序言 项目主要使用oracle但是我不太喜欢其他编程语言,加上可能需要用python部署算法包,从oracle表中读出数据,处理完成后在放回oracle中去,所以在windows上就想到先用pyt ...

  6. Android-FloatingActionButton

    Android-FloatingActionButton android-floating-action-button 我的地址:https://github.com/kongqw/android-f ...

  7. 如何找出Xcode中不同版本Swift的路径

    我们知道Xcode中可能包含不知一个Swift的版本,那么我们如何找到它们对应的路径呢? 熟悉unix shell命令的童鞋都知道有一个find指令,在我们已知Xcode路径时,我们可以在其中找到Sw ...

  8. ROS(indigo) 用于机器人控制的图形化编程工具--code_it robot_blockly

    0 简介: 编程语言有汇编,高级语言,解释语言等,现在图形化编程也越来越流行.图形化编程简单易学.8年前,微软推出了VPL用于机器人程序设计,如Python和JavaScript都可以用图形化框图实现 ...

  9. iOS下使状态栏颜色与H5中背景色一致

    iOS 中有的页面也能会内嵌WebView,然后WebView中用H5做了一个导航,而iOS 中状态栏的颜色很难调整的与H5中导航颜色一致.如下图所示: 其实出现这种原因,主要是因为使用16进制颜色, ...

  10. 为什么选择C++

    为什么选择C++,怎么不选其它语言呢? 为什么不选择C? 因为C++比C简单点~ 为什么不选择C#? 因为C++可以在所有操作系统上使用. 为什么不选择JAVA? 因为C++的性能好一点~ 还有其他的 ...