Factorial Trailing Zeroes

时间:2022-05-23 02:13:24

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的更多相关文章

  1. 【LeetCode】172&period; Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

  2. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

  3. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

  4. LeetCode 172&period; 阶乘后的零&lpar;Factorial Trailing Zeroes&rpar;

    172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...

  5. LeetCode&lowbar;172&period; Factorial Trailing Zeroes

    172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...

  6. LeetCode172 Factorial Trailing Zeroes&period; LeetCode258 Add Digits&period; LeetCode268 Missing Number

    数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...

  7. &lbrack;LeetCode&rsqb; Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  8. LeetCode Factorial Trailing Zeroes

    原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...

  9. &lbrack;LeetCode&rsqb; 172&period; Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  10. 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 ...

随机推荐

  1. 记一次js中和php中的字符串长度计算截取的终极问题和完美解决方案

    1.js是用unicode算长度的,比如单字节的算1,中文也算1,但是正常我们想让两个单字节算1,如何计算这个长度 第一种解决方案,用正则,如下 /[\u0x00-\u0xff]/,天真的想着,这样就 ...

  2. unity3d插件Daikon Forge GUI 中文教程-1-Daikon Forge介绍

    (游戏蛮牛首发)大家好我是孙广东官网提供了专业的视频教程http://www.daikonforge.com/dfgui/tutorials/,只是是在youtube上,要观看是须要FQ的. 只是教程 ...

  3. 在Android Studio上测试运行,Unity发布成Android包过程中所遇到的问题及解决方案

    问题一:Exception: JNI: Init'd AndroidJavaObject with null ptr 解决方法: 所有关于JNI出现的问题,只有三种错误存在,第一是需要在真机上运行测试 ...

  4. Python随机字符串验证码

    def code_get(self): source = ['] #数字验证码 # source = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J', ...

  5. JS身份证验证

    window.checkIdcard = function (idcard) { var errors = new Array( "yes", "请检查输入的证件号码是否 ...

  6. VUE脚手架,babel转码 常用命令

    vue-cli脚手架,单页面应用初始化时 npm -v 查看npm版本号 npm install vue-cli -g  全局安装vue-cli vue -V查看vue版本号,说明vue-cli已经安 ...

  7. oracle主键约束、唯一键约束和唯一索引的区别

    (1)主键约束和唯一键约束均会隐式创建同名的唯一索引,当主键约束或者唯一键约束失效时,隐式创建的唯一索引会被删除: (2)主键约束要求列值非空,而唯一键约束和唯一索引不要求列值非空: (3)相同字段序 ...

  8. hibernate方言

    hibernate中,方言是解决hql翻译成sql保证语法的正确,首先数据库都是支持sql的,不同的数据库会存在一些语法上面的差异.HQL则是基于对象的查询语言,当系统需要数据库的变换时,那么用hib ...

  9. spin&lowbar;USACO

    Spinning Wheels1998 ACM NE Regionals Each of five opaque spinning wheels has one or more wedges cut ...

  10. is 和 &equals;&equals; 的区别&comma;utf和gbk的转换&comma;join用法

    is 和 == 的区别 # is 比较的是内存地址 # == 比较的是值 a = 'alex' b = 'alex' #int,str(小数据池)会被缓存,为了节约内存 print(id(a),id( ...