Java [Leetcode 172]Factorial Trailing Zeroes
题目描述:
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
解题思路:
对于阶乘而言,也就是1*2*3*...*n
[n/k]代表1~n中能被k整除的个数
那么很显然
[n/2] > [n/5] (左边是逢2增1,右边是逢5增1)
[n/2^2] > [n/5^2](左边是逢4增1,右边是逢25增1)
……
[n/2^p] > [n/5^p](左边是逢2^p增1,右边是逢5^p增1)
随着幂次p的上升,出现2^p的概率会远大于出现5^p的概率。
因此左边的加和一定大于右边的加和,也就是n!质因数分解中,2的次幂一定大于5的次幂
代码如下:
public class Solution {
public int trailingZeroes(int n) {
int res = 0;
while(n > 0){
res += n / 5;
n /= 5;
}
return res;
}
}
Java [Leetcode 172]Factorial Trailing Zeroes的更多相关文章
- 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 ...
- 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 ...
- ✡ 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 ...
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Leetcode 172 Factorial Trailing Zeroes
给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...
- leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)
数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...
- [LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数
所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125.. ...
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
随机推荐
- 网站常用css必备css reset
在我们写前端代码页面的时候,很多常用的CSS类都是固定的!但没有一个标准或者大家都按自己的方式去随意的写,这样就每次都重复写一些固定的类! 为此HTML5 Doctor(HTML5医生)为我们总结了一 ...
- jquery获取标签内容,编辑内容
一.获取页面元素 三种方式获取页面中元素的内容. input标签使用:.val()获取 标签下的html及文本内容:.html() 仅获取标签下的纯文本内容:.text() <head> ...
- ssh连接慢
suse刚装完,开始用ssh的时候,总会遇到这样的问题:输入了用户名以后,等半天才出输入密码的框,很是急人.这是dns反查造成的.解决方法:编辑 /etc/ssh/sshd_conf , 将 #Use ...
- PHP之mysql_real_escape_string()函数讲解
定义和用法 mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符. 下列字符受影响: \x00 \n \r \ ' " \x1a 如果成功, ...
- Android 异步加载
Android 4.0 后 貌似规定了 在主线程中不允许访问网络,在子线程中不允许修改UI. 否则会抛出NetworkOnMainThreadException 异常 解决办法: 采用继承 Async ...
- asp.net后台获取路径的各种方法归纳
asp.net后台获取路径的各种方法归纳 1.Request.CurrentExecutionFilePath 获取当前请求的虚拟路径,不同于 FilePath,差别在于如果请求已在服务器代 ...
- 安装ubuntu vi编辑无法正常使用的时候 如方向键变成ABCD
http://blog.sina.com.cn/s/blog_7e3f6e8f0100vkon.html 在使用ubuntu的时候,发现vi编辑模式下退格键backspace和上下左右光标移动键不能用 ...
- Photoshop CS4序列号过期的问题
1)在网络上搜寻一些PS CS4序列号: 如1330-1221-6824-4838-0308-6823,1330-1283-7461-4574-7002-2504,1330-1795-2880-537 ...
- ConcurrentHashMap使用示例
ConcurrentHashMap使用示例 发表于2年前(2013-07-12 19:05) 阅读(3660) | 评论(0) 25人收藏此文章, 我要收藏 赞5 如何快速提高你的薪资?-实力拍“ ...
- tomcat中如何运行war包呢
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPQAAADRCAIAAAB0LAgsAAAQtklEQVR4nO2d7W/bxh3H9ZfZbRrFOj