本文是在学习中的总结,欢迎转载但请注明出处: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. 聊聊jstack的工作原理

    实现一个jstack 在聊Jstack得工作原理前呢,不如让我们先写一个简单的jstack玩玩.不用怕,很简单的,就几行代码的事,看: public class MyJstack { public s ...

  2. Docker常见仓库Redis

    Redis 基本信息 Redis 是开源的内存 Key-Value 数据库实现. 该仓库提供了 Redis 2.6 ~ 2.8.9 各个版本的镜像. 使用方法 默认会在 6379 端口启动数据库. $ ...

  3. [BBS]搭建开源论坛之JForum安装使用札记

    本文作者:sushengmiyan 本文地址:http://blog.csdn.net/sushengmiyan/article/details/47761303 目录 目录 BBS搭建开源论坛之JF ...

  4. SSH 之 Spring的源码(一)——Bean加载过程

    看看Spring的源码,看看巨人的底层实现,拓展思路,为了更好的理解原理,看看源码,深入浅出吧.本文基于Spring 4.0.8版本. 首先Web项目使用Spring是通过在web.xml里面配置 o ...

  5. Android开发学习之路--Drawable mutations

      时间过得很快,明天终于可以拿到房子了,交完这次房租,也可以成为房东了,看看博客也好久没有更新了,最近一直在整机器人,也没有太多时间整理博客.   今天下午和同事一起遇到了一个问题,就是明明没有改变 ...

  6. YYModel V1.0.4源码解析

    YYKit出现了很长时间了,一直想要详细解析一下它的源码,都是各种缘由推迟了. 最近稍微闲了一点,决定先从最简单的YYModel开始吧. 首先,我也先去搜索了一下YYModel相关的文章,解析主要AP ...

  7. paypal的IPN机制

    paypal对接时发现有这么一个机制,看起来还不错,起到了防止篡改欺诈行为,保证了通信的安全性,但会增加几次通信.

  8. 在电脑上安装Linux操作系统

    1硬件需求 A 一台电脑 B 一个优盘 2软件需求 A制作优盘启动盘的软件PowerISO BLinux操作系统的镜像文件 3安装PowerISO,并使用PowerISO A安装PowerISO B插 ...

  9. Java基本语法-----java进制的转换

    进制: 进制是一种记数方式 ,可以用有限的数字符号代表所有的数值.由特定的数值组成. 1整型的表现形式 1.十进制: 都是以0-9这九个数字组成,不能以0开头. 2.二进制: 由0和1两个数字组成. ...

  10. UNIX网络编程——原始套接字(dos攻击)

    原始套接字(SOCK_RAW).应用原始套接字,我们可以编写出由TCP和UDP套接字不能够实现的功能. 注意原始套接字只能够由有 root权限的人创建. 可以参考前面的博客<<UNIX网络 ...