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 ...
随机推荐
- 自定义Attribute 服务端校验 客户端校验
MVC 自定义Attribute 服务端校验 客户端校验/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Autho ...
- Oracle VM VirtualBox 设置本机与虚拟机共享文件
文章转载自http://wenku.baidu.com/link?url=5uZ1xWnGC55FGW2XUqzLVdttRcik2xCqwRKEdh6z-nZJ63UQn3j2750ES_q4Oro ...
- 分析MapReduce执行过程
分析MapReduce执行过程 MapReduce运行的时候,会通过Mapper运行的任务读取HDFS中的数据文件,然后调用自己的方法,处理数据,最后输出. Reducer任务会接收Mapper任务输 ...
- 基于jQuery打造的选项卡向上弹出jquery焦点图切换特效
基于jQuery打造的选项卡向上弹出jquery焦点图切换特效 鼠标经过标题栏,会出现层特效向上滑动,并且在同时进行幻灯片切换,效果十分不错. 有兴趣的童鞋可以下载看看,在IE6方面兼容性也不错,只有 ...
- C++判断五位以内的对称素数
题目内容:判断一个数是否为对称且不大于五位数的素数. 输入描述:输入数据含有不多于50个的正整数n(0<n<232). 输出描述:对于每个n,如果该数是不大于五位数的对称素数,则输出“Ye ...
- Java实现计算20的阶乘
循环从1乘到20,要注意的就是结果可能会很大,长度超出int类型的范围,所以定义乘积的时候用long. 代码如下: public class Practice3 { public static voi ...
- 使APP消除上方手机消息提示栏(显示WIFI,信号格那栏)消失的方法
public void toggleFullscreen(boolean fullScreen) { // fullScreen为true时全屏,否则相反 WindowManager.LayoutPa ...
- 设计师眼中功能强大的Xcode
作为设计师,不仅要能创造出移动为先的新产品,更要了解能创造出优秀移动作品的工具.这个实现过程可以让我们的设计更加优秀. 过去两个月,我每天在 Xcode 上花费的时间大约有 10 个小时,我学到了很多 ...
- 【标题】一本帮你提高Excel办公效率的VBA书
公司工程部男同事,EXCEL能力最强的前三位,分别号称:大表哥 二表哥 三表哥 公司财务部女同事,EXCEL能力最强的前三位,分别号称:大表姐 二表姐 三表姐 想成为你们公司的“表哥”.“表姐”? 但 ...
- wamp环境搭建
php 进入php安装目录. 修改php.ini-developement为php.ini 配置扩展目录为你的PHP安装目录下的ext目录,在我这里是 extension_dir = "C: ...