/*
  * Problem 172: Factorial Trailing Zeroes
  * Given an integer n, return the number of trailing zeroes in n!.
  * Note: Your solution should be in logarithmic time complexity.
  */

  /*
   * Solution 1
   * 对于每一个数字,累计计算因子10、5、2数字出现的个数,结果等于10出现的个数,加上5和2中出现次数较少的
   * 改进:5出现的次数一定大于2出现的次数,因此只需要统计因子10和5出现的次数。进一步衍生为只统计5出现的次数。
   * 改进:讲循环控制的步长改为5
   */
 int trailingZeroes(int n) {
     ;
     ;
     ;

     int temp;
     ; i--) {
         temp = i;
         ) {
              == ) {
                 number10++;
                 temp = temp/;
             }  == ) {
                 number5++;
                 temp = temp/;
 //            } else if (temp % 2 == 0) {
 //                number2++;
 //                temp = temp/2;
             } else {
                 break;
             }
         }
     }
     return (number5>number2?number2:number5)+number10;
 }

 /*
  * Solution 2
  * 根据上面分析,只需要统计所有数字中因子5出现的次数
  */
 int trailingZeroes(int n) {
     ;
     ;
     ) {
         result += temp;
         temp = temp/;
     }
     return result;
 }

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. LeetCode——Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  2. 如何统一删除word中的超链接

    [摘要] 我们从别处拷贝文字,或从网上复制的文字,里面有很多超级链接,如何可以批量删除这些链接呢?这里介绍两种批量删除链接的方法. [正文] 方法一:使用快捷键删除超链接 有个神奇的快捷键,可以帮我们 ...

  3. linux下SSH远程连接服务慢解决方案

    1.适用命令及方案如下:[远程连接及执行命令]ssh -p22root@10.0.0.19ssh -p22 root@10.0.0.19 /sbin/ifconfig[远程拷贝:推送及拉取]scp - ...

  4. spring入门:beans.xml不提示、别名、创建对象的三种方式

    spring的版本是2.5 一.beans.xml文件不提示 Location:spring-framework-2.5.6.SEC01\dist\resources\spring-beans-2.5 ...

  5. 基类的参考Expression能传一个lambda表达式

    using System;using System.Collections.Generic;using System.Data.Entity.Infrastructure;using System.L ...

  6. java替换字符串和用indexof查找字符

    java自带替换 String s="hlz_and_hourui哈哈"; String new_S=s.replaceAll("哈", "笑毛&qu ...

  7. UIPickerView(选择控制器) 自学之初体验

    UIPickerView 是一个选择器控件, 它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活.UIPickerView 直接继承了 UIView  ...

  8. mysql学习(补充)

    修改表名 rename table olderName to newerName; \c 结束不执行 设置字符集 set names gbk; mysql类型 数值型 属性修饰符 zerofill u ...

  9. 在controller间分享数据(第一种办法)

    Blood 血腥的方法 每个controller都有自己的scope, 同时也可以共享他们老爸的scope内的数据.如果我们想让两个controller共享数据的化, 有多种方法. 最直接血腥的就是在 ...

  10. tokyocabinet安装日志(持续更新)

    http://sourceforge.jp/projects/sfnet_tokyocabinet/releases/这个网站的最新tt和tc都在此1.下载tokyocabinethttp://sou ...