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 ...
随机推荐
- Where is "Active Directory Information Extractor"?
My friend she showed me a screenshot as below yesterday. The name of this document is “EnCase Forens ...
- APUE第一章_课后习题
/* 未完成的:1.5 不过在下文中已经给出了解答. */ 1.1 在系统上查证,除根目录外,目录.和..是不同的 ans:这个很容易,用vim打开.和..就可以看到区别. 1.2 分析程序清单1-4 ...
- JavaScript中的Get和Set访问器
今天要和大家分享的是JavaScript中的Get和Set访问器,和C#中的访问器非常相似. 标准的Get和Set访问器的实现 function Field(val){ this.va ...
- Android IOS WebRTC 音视频开发总结(三八)-- tx help
本文主要介绍帮一个程序员解决webrtc疑问的过程,文章来自博客园RTC.Blacker,支持原创,转载请说明出处(www.rtc.help) 这篇文章内容主要来自邮件,为什么我会特别整理到随笔里面来 ...
- 数据库mysql的基本命令
问题分析 当数据量很大的时候,所有数据都集中在一个文本文件中的话,读写会很困难,内存消耗大,速度很慢 操作很麻烦,因为读写都要根据指定的格式尽心解析,不通用 每次获取数据都要全部数据重新读写,不能通过 ...
- HLS入门收集(1)
使用HLS各种问题 关于求指数函数 exp(x) 在HLS中使用exp(x),也就是指数函数.不能导出RTL到EDK也就是Pcore 只能导出为VIVADO IP:相关解释:见官方论坛 http:/ ...
- 苹果App转移图文详解-Transfer App
此文章只是为了记录一个Apple ID下的APP,转移到另外一个Apple ID 账户下.为了说的清楚下面用A账户(有App,要转出去)B账户(接收A账户App,接收者),来说明. 1. 登 ...
- (五)、nodejs使用bootstrap的样式进行分页
一.page方法 /****************************************************** * Created User: * Created Time: 201 ...
- vue.js笔记
一.v-bind 缩写 <!-- 完整语法 --> <a v-bind:href="url"></a> <!-- 缩写 --> &l ...
- IIS 7.5 配置Asp+Access的几点注意的地方
环境:window2008 R2 + iis 7.51 把网站程序放在一个www文件夹里面,给这个文件夹添加everyone的用户,赋予全部读写权限,这样安全些.2 选中要配置的网站,点击页面中间“A ...