1. /*
  2. * Problem 172: Factorial Trailing Zeroes
  3. * Given an integer n, return the number of trailing zeroes in n!.
  4. * Note: Your solution should be in logarithmic time complexity.
  5. */
  6.  
  7. /*
  8. * Solution 1
  9. * 对于每一个数字,累计计算因子10、5、2数字出现的个数,结果等于10出现的个数,加上5和2中出现次数较少的
  10. * 改进:5出现的次数一定大于2出现的次数,因此只需要统计因子10和5出现的次数。进一步衍生为只统计5出现的次数。
  11. * 改进:讲循环控制的步长改为5
  12. */
  13. int trailingZeroes(int n) {
  14. ;
  15. ;
  16. ;
  17.  
  18. int temp;
  19. ; i--) {
  20. temp = i;
  21. ) {
  22. == ) {
  23. number10++;
  24. temp = temp/;
  25. } == ) {
  26. number5++;
  27. temp = temp/;
  28. // } else if (temp % 2 == 0) {
  29. // number2++;
  30. // temp = temp/2;
  31. } else {
  32. break;
  33. }
  34. }
  35. }
  36. return (number5>number2?number2:number5)+number10;
  37. }
  38.  
  39. /*
  40. * Solution 2
  41. * 根据上面分析,只需要统计所有数字中因子5出现的次数
  42. */
  43. int trailingZeroes(int n) {
  44. ;
  45. ;
  46. ) {
  47. result += temp;
  48. temp = temp/;
  49. }
  50. return result;
  51. }

LeetCode Day4——Factorial Trailing Zeroes的更多相关文章

  1. [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数

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

  2. LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)

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

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

  4. 【leetcode】Factorial Trailing Zeroes

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

  5. ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java

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

  6. 【leetcode】Factorial Trailing Zeroes(easy)

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

  7. Java for LeetCode 172 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

    Given an integer n, return the number of trailing zeroes in n!. 最初的代码 class Solution { public: int t ...

  9. Java [Leetcode 172]Factorial Trailing Zeroes

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

随机推荐

  1. gdb 调试coredump文件过程

    gdb 调试coredump文件过程: 第一步:首先需要一个进程的coredump文件,怎么搞出coredump文件呢? 1. ps -fax|grep                 进程名称 找到 ...

  2. [Spring boot] web应用返回jsp页面

    同事创建了一个spring boot项目,上传到svn.需要我来写个页面.下载下来后,始终无法实现在Controller方法中配置直接返回jsp页面. 郁闷了一下午,终于搞定了问题.在此记录一下. 目 ...

  3. IOS拷贝文件到沙盒

    - (void)copyFileFromResourceTOSandbox { //文件类型 NSString * docPath = [[NSBundle mainBundle] pathForRe ...

  4. Pasha and String(思维,技巧)

    Pasha and String Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  5. [jQuery] 自做 jQuery Plugin - Part 1

    有時候寫 jQuery 時,常會發現一些簡單的效果可以重複利用.只是每次用 Copy & Paste 大法似乎不是件好事,有沒有什麼方法可以讓我們把這些效果用到其他地方呢? 沒錯,就是用 jQ ...

  6. HTTP请求的TCP瓶颈分析[转]

    阅读目录 延迟的因素 速度延时 带宽延时 最后一公里延时-tracerouter 目标 rwnd的设置 慢启动过程 慢启动的影响 慢启动对HTTP影响的一次计算 拥塞窗口的合适值 服务器配置调优 应用 ...

  7. [裸KMP][HDU1711][Number Sequence]

    题意 找到子串在母串出现的第一个位置 解法 裸的KMP 特别的地方 第一次不看模板自己敲的KMP #include<stdio.h> const int maxn=100000; cons ...

  8. hdu2095 像水题的不错题 异或运算

    异或运算的基础有点忘记了 先介绍一下..2个数异或 就是对于每一个二进制位进行位运算 具有2个特殊的性质 1.一个数异或本身恒等于0,如5^5恒等于0: 2.一个数异或0恒等于本身,如5^0恒等于5. ...

  9. 【Java基础】几种简单的调用关系与方法

    直接上代码吧. class lesson4AB //同一个类下的public修饰的方法A,B可以相互调用 { public void A() { B();//等价于this.B(); } public ...

  10. .NET进阶系列之一:C#正则表达式整理备忘

    有一段时间,正则表达式学习很火热很潮流,当时在CSDN一天就能看到 好几个正则表达式的帖子,那段时间借助论坛以及Wrox Press出版的<C#字符串和正则表达式参考手册>学习了一些基础的 ...