Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
只有2 * 5才会产生0只要计算出因子中2和5的个数取小的的那个就好了
public class Solution {
public int trailingZeroes(int n) {
int numsOf2 = 0;
int numsOf5 = 0;
for(int i = 2; i <= n; i++){
numsOf2 += get2nums(i);
numsOf5 += get5nums(i);
}
return numsOf2 < numsOf5 ? numsOf2 : numsOf5;
} /**
* 计算num中2作为乘积因子的个数
* @param num
* @return
*/
private int get2nums(int num){
int numsOf2 = 0;
if(num % 2 != 0)
return 0;
else{
while(num % 2 == 0){
num = num / 2;
numsOf2 ++;
}
}
return numsOf2;
} /**
* 获取5的个数
* @param num
* @return
*/
private int get5nums(int num){
int numsOf5 = 0;
if(num % 5 != 0)
return 0;
else{
while(num % 5 == 0){
num = num / 5;
numsOf5 ++;
}
}
return numsOf5;
}
}
Factorial Trailing Zeroes的更多相关文章
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...
- LeetCode_172. Factorial Trailing Zeroes
172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...
- LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
数学题 172. 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] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
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 ...
随机推荐
- Becoming a Hacker...
This is my dream... http://catb.org/~esr/faqs/hacker-howto.html 黑客的精神 世上仍有大量迷人的事情等待解决 同样的问题不应被重复处理两次 ...
- linux device model简述
参考: 1)<LINUX设备驱动程序>第十四章 Linux 设备模型 2)内核源码2.6.38 内核初始化的时候会对设备模型作初始化,见init/main.c: start_kernel- ...
- Div 不换行、垂直居中等样式
1. Div内文本过长不换行 1.1 文本不换行 超出部分显示"..." .style1 { float:left; white-space:nowrap; text-overfl ...
- iptables用法
iptables -t nat -A PREROUTING -s 10.10.10.0/24 -i eth1 -p tcp --dport 80 -j REDIRECT --to-ports 3128 ...
- SMTP Failed on Process Scheduler
If you are seeing messages like this in your message log when running a process through the process ...
- Winform登录、控制软件只运行一次、回车登录
Winform登录对很多程序猿来说都有些困惑,登录进入主窗体后要销毁登录窗体,而不是隐藏哦,怎么实现呢? 先贴一段Program.cs的代码 static void Main() { Mutex mu ...
- nginx的rewrite,gzip,反向代理学习笔记
rewrite模块名:ngx_http_rewrite_module默认自动被编译 指令:rewrite regex replacement [flag] regex :正则表达式,用于匹配用户请求的 ...
- 深入浅出MongoDB(二)概述
上次的博文深入浅出MongoDB(一)NoSQL中我们已经简单介绍了一下NoSQL的基本概念,这次我们来了解一下MongoDB的相关概念. 1.简介 MongoDB是一款由C++编写的高性能.开源.无 ...
- Windows7防火墙服务无法启用怎么办
点击windows 7控制面板中防火墙的“推荐配置”没有反应,打开“服务”,无法启动windows firewall,并报错. 问题: 1.点击windows 7控制面板中防火墙的“推荐配置”没有 ...
- java中4中类修饰符访问范围
public:本类中可见,同包中可见,子类中可见,其他包中可见. protected:本类中可见,同包中可见,子类中可见,其他包不可见. 默认:本类中可见,同包中可见,子类不可见,其他包不可见. pr ...