[LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!.
Example 1:
Input: 3
Output: 0
Explanation: 3! = 6, no trailing zero.
Example 2:
Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Note: Your solution should be in logarithmic time complexity.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
这道题并没有什么难度,是让求一个数的阶乘末尾0的个数,也就是要找乘数中 10 的个数,而 10 可分解为2和5,而2的数量又远大于5的数量(比如1到 10 中有2个5,5个2),那么此题即便为找出5的个数。仍需注意的一点就是,像 25,125,这样的不只含有一个5的数字需要考虑进去,参加代码如下:
C++ 解法一:
class Solution {
public:
int trailingZeroes(int n) {
int res = ;
while (n) {
res += n / ;
n /= ;
}
return res;
}
};
Java 解法一:
public class Solution {
public int trailingZeroes(int n) {
int res = 0;
while (n > 0) {
res += n / 5;
n /= 5;
}
return res;
}
}
这题还有递归的解法,思路和上面完全一样,写法更简洁了,一行搞定碉堡了。
C++ 解法二:
class Solution {
public:
int trailingZeroes(int n) {
return n == ? : n / + trailingZeroes(n / );
}
};
Java 解法二:
public class Solution {
public int trailingZeroes(int n) {
return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
}
}
Github 同步地址:
https://github.com/grandyang/leetcode/issues/172
类似题目:
Preimage Size of Factorial Zeroes Function
参考资料:
https://leetcode.com/problems/factorial-trailing-zeroes/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数的更多相关文章
- [LeetCode] 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 (阶乘末尾零的数量)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数
LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...
- 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- 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 ...
- 172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- ✡ 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 ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- Leetcode 172 Factorial Trailing Zeroes
给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...
随机推荐
- Program 3 – CS 344
Program 3 – CS 344OverviewIn this assignment you will write your own shell in C, similar to bash. No ...
- LCM Walk HDU - 5584
A frog has just learned some number theory, and can't wait to show his ability to his girlfriend. No ...
- Nginx Cache-Control
转自:https://www.cnblogs.com/sfnz/p/5383647.html HTTP协议的Cache-Control指定请求和响应遵循的缓存机制.在请求消息或响应消息中设置 Cach ...
- 初探云原生应用管理(二): 为什么你必须尽快转向 Helm v3
系列介绍:这个系列是介绍如何用云原生技术来构建.测试.部署.和管理应用的内容专辑.做这个系列的初衷是为了推广云原生应用管理的最佳实践,以及传播开源标准和知识.在这个系列文章的开篇初探云原生应用管理(一 ...
- Vue基础框架
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <!-- 设置语言为 ...
- kali渗透综合靶机(十三)--Dina 1.0靶机
kali渗透综合靶机(十三)--Dina 1.0靶机 一.主机发现 1.netdiscover -i eth0 -r 192.168.10.0/24 二.端口扫描 1. masscan --rate= ...
- matplotlib基础
Matplotlib 基础 注:本文中的程序都默认引入了numpy库和matplotlib库,并且分别简写为np与plt:如果读者不知道怎么使用numpy库,可以移步到这一博客上进行简单的学习 一.简 ...
- SQL Server中,如何查看每个数据库的Owner是哪个SQL Server账户,也就是谁创建的
有时候我们作为SQL Server的DBA,会需要查找每个数据库的Owner是哪个SQL Server账户,也就是谁创建的. 我们可以使用系统存储过程"sys.sp_helpdb" ...
- SpringBoot打可执行war包
1. 主程序类: @SpringBootApplication public class Application extends SpringBootServletInitializer { @Ove ...
- mysql判断是否包含某个字符的方法
mysql判断是否包含某个字符的方法用locate 是最快的,like 最慢.position一般实战例子:select * from historydatawhere locate('0',open ...