[LeetCode] Factorial Trailing Zeros
Well, to compute the number of trailing zeros, we need to first think clear about what will generate a trailing 0? Obviously, a number multiplied by 10 will have a trailing 0 added to it. So we only need to find out how many 10's will appear in the expression of the factorial. Since 10 = 2 * 5and there are a bunch more 2's (each even number will contribute at least one 2), we only need to count the number of 5's.
Now let's see what numbers will contribute a 5. Well, simply the multiples of 5, like 5, 10, 15, 20, 25, 35, .... So is the result simply n / 5? Well, not that easy. Notice that some numbers may contribute more than one 5, like 25 = 5 * 5. Well, what numbers will contribute more than one 5? Ok, you may notice that only multiples of the power of 5 will contribute more than one 5. For example, multiples of 25 will contribute at least two 5's.
Well, how to count them all? If you try some examples, you may finally get the result, which is n / 5 + n / 25 + n / 125 + .... The idea behind this expression is: all the multiples of 5 will contribute one 5, the multiples of 25 will contribute one more 5 and the multiples of 125 will contribute another one more 5... and so on. Now, we can write down the following code, which is pretty short.
class Solution {
public:
int trailingZeroes(int n) {
int count = ;
for (long long i = ; n / i; i *= )
count += n / i;
return count;
}
};
[LeetCode] Factorial Trailing Zeros的更多相关文章
- [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数
LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- LeetCode Factorial Trailing Zeroes
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...
- 关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解
题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0 ...
- [LeetCode] Factorial Trailing Zeroes 阶乘末尾0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Python3解leetcode Factorial Trailing Zeroes
问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 ...
- LeetCode Factorial Trailing Zeroes (阶乘后缀零)
题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...
- 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe
Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...
随机推荐
- jBoss修改端口号
http://blog.csdn.net/ghost_t/article/details/5708991 jBoss版本: jboss-5.1.0.GA jboss-6.0.0.Final j ...
- vs2017搭建自己的nuget服务器
准备环境 vs2017 第一步 创建一个新的asp.net 空网站 .net框架使用4.6以上版本 (或者在第二部中使用低版本的nuget server) 第二步 打开nuget包管理器 搜索nu ...
- 补知识:EntityFramework Core映射关系详解
前言 本节我们回归下EF Core基础,来讲述EF Core中到底是如何映射的,废话少说,我们开始. One-Many Relationship(一对多关系) 首先我们从最简单的一对多关系说起,我们给 ...
- Atitit.软件开发的最终的设计 dsl化,ast化(建立ast, 解析执行ast)
Atitit.软件开发的最终的设计 dsl化,ast化(建立ast, 解析执行ast) 1. 使用js,html 撰写dsl1 1.1. 架构图1 1.2. html2 1.3. Js2 1.4. C ...
- MVC之旅(1)
其实很久很久之前就有接触asp.net mvc了.那些年,还是在大学校园的美好青葱时光,常常听到一些大牛开口闭口都是mvc,心痒痒的也去图书馆借了本mvc的教材,应该是mvc 2.悲催的是,我都没知道 ...
- JSP、servlet、SQL三者之间的数据传递
JSP.servlet.SQL三者之间的数据传递 博客分类: web开发 JSPservletSQL数据库连接池web开发 前言: 最近一直在做WEB开发,现总结一下这一段时间的体会和感触. 切记, ...
- Jquery学习笔记(10)--ajax删除用户,使用了js原生ajax
主要复习了php的pdo数据库操作,和js的ajax,真麻烦,希望jquery的ajax简单点. index.php: <!DOCTYPE html> <html lang=&quo ...
- TCP/IP模型
TCP/IP是一组用在实现网络相互连接的通信协议,internet网络体系结构就是以TCP/IP为核心的. TCP/IP分成了四个层次 1.应用层 应用层对应与OSI参考模型的高层,为用户提供了所需要 ...
- Retrofit--官网2.1.0
Retrofit--官网2.1.0 android Retrofit 介绍 API 描述 请求方法 URL 处理 请求体 表单的 ENCODED 和 MULTIPART HEADER 处理 同步 VS ...
- jquery Ajax Queue 队列实现
有时候我们需要按顺序调用一组ajax,这些ajax需要有先后顺序,类似于同步的ajax,那么我们可以通过以下的方式来实现: (这个Ajax用到jQuery.post) //定义一个AJAX队列 $.n ...