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. 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. 阶乘后的零(Factorial Trailing Zeroes)

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

  5. LeetCode_172. 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. LeetCode258 Add Digits. LeetCode268 Missing Number

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

  7. [LeetCode] 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. [LeetCode] 172. 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. ASP.NET验证控件一

    为了更好地创建交互式Web应用程序.加强应用程序安全性,程序开发人员应该对用户输入的内容进行验证. ASP.NET提供了一系列输入验证控件,使用这些控件用户可以很方便地实现输入验证. ASP.NET还 ...

  2. Android中SearchView修改字体颜色

    首先获取searchView控件,比如在actionbar上获取: SearchView searchView = (SearchView) menu.findItem(R.id.action_sea ...

  3. php通过正则从字符串中获取所有图片url地址

    /** * 提取字符串中图片url地址 * @param type $str * @return type */ function getimgs($str) { $reg = '/((http|ht ...

  4. windows下如何修改远程登录端口

    windows下如何修改远程登录端口 windows远程桌面默认端口为3389,修改 方法如下:在"开始>运行"中输入"regedit" 点击“确定”,打 ...

  5. Windows2003屏蔽IP

    1.打开本地安全策略   2.创建新的IP策略   去掉勾选向导  我们编辑 直接右键指派   指派可以看出来生效...网络已经不通了              

  6. Java通过Executors提供四种线程池

    http://cuisuqiang.iteye.com/blog/2019372 Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果 ...

  7. scala学习笔记1

    一.REPL scala解释器读到一个表达式,对它进行求值,将它的打印出来,接着再继续读下一个表达式.这个过程被称作 读取-打印-循环,即REPL. 从技术上讲,scala程序并不是一个解释器.实际发 ...

  8. mr的logs的查看

    在map或者reduce函数中使用System.out.println打印的信息沾满查看呢? 步骤1:启动history server /usr/local/hadoop-2.6.0/sbin/mr- ...

  9. .ascx.g.cs文件不能生成 The name ‘InitializeControl’ does not exist in the current context - Visual Web part Sharepoint

    InitializeControl doesn't exsit When using visual studio 2012 for developing SharePoint 2013 Visual ...

  10. 清理sql2000查询分析器登录名记录

    注册表 HKEY_CURRENT_USER/Software/Microsoft/Microsoft  SQL  Server/80/Tools/Client/PrefServers