题目:

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

思路:

  • 题意是要求一个数字的阶乘,末尾有多少个0
  • 要求是对数级别的时间,所以考虑用递归
  • 分析一下,产生一个10,后面加0,找到所有的2*5,或者2的次方×5的次方,任何情况下因子2的个数永远大于5
  • 所以只需要计算因子5的个数,(25*4 = 100),算2个5
  • -

代码:

class Solution {
    /*
     * param n: As desciption
     * return: An integer, denote the number of trailing zeros in n!
     */
    public long trailingZeros(long n) {
        // write your code here
        return n / 5 == 0 ? 0 : n /5 + trailingZeros(n / 5);
    }
};

暴力方法:(不推荐)

public class Solution {
    public int trailingZeroes(int n) {
        int count = 0;
        if(get(n) == 0){
            return 1;
        }
        int sum = get(n);
        while(sum > 0){
            if(sum % 10 == 0){
                count++;
            }
            sum = sum /10;
        }
        return sum;
    }
    public int get(int n){
        int all = 0;
        if(n == 0){
            return 0;
        }
        for(int i = 1;i <= n;i++){
            all = all*i;
        }
        return all;
    }
}

LeetCode(31)-Factorial Trailing Zeroes的更多相关文章

  1. LeetCode Day4——Factorial Trailing Zeroes

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

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

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

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

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

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

  5. 【leetcode】Factorial Trailing Zeroes

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

  6. ✡ 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 ...

  7. 【leetcode】Factorial Trailing Zeroes(easy)

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

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

  9. leetcode:Factorial Trailing Zeroes

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

随机推荐

  1. (一二四)tableView的多组数据展示和手动排序

    最近在写一个轻量级的网络游戏,遇到了技能优先顺序手动排序的需求,我就想到了iOS自带的tableView编辑功能,对其进行了初步探索,最后做出的效果如下图所示: 点击左边可以删除,拖住右边可以手动排序 ...

  2. Android的DatePicker和TimePicker-android学习之旅(三十八)

    DatePicker和TimePicker简介 DatePicker和TimePicker是从FrameLayout继承而来,他们都是比较简单的组件.时间改变时间分别添加OnDateChangeLis ...

  3. 学习笔记-JS公开课三

    DOM技术概述 DOM : DocumentObject Model 将HTML标记型文档,封装成对象,提供更多的属性和行为 DOM的三级模型 第一级:将标记型文档,封装成对象,提供更多的属性和行为 ...

  4. Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 下面看一下CatSprite中最复杂的moveToward方法, ...

  5. HMM:隐马尔科夫模型-维特比算法

    http://blog.csdn.net/pipisorry/article/details/50731584 目标-解决HMM的基本问题之二:给定观察序列O=O1,O2,-OT以及模型λ,如何选择一 ...

  6. ROS(indigo)国外开源示例包括多机器人控制等基于V-Rep和Gazebo的仿真

    ROS(indigo)国外开源示例包括多机器人的V-Rep和Gazebo仿真等 1 micros_swarm_framework 使用超级经典的stage. http://wiki.ros.org/m ...

  7. 基于xml 实现动态加载权限功能树列表---EFSFrame企业级开发架构

    在学习EFSFrame框架的过程中,感触最深的就是通过xml来实现前台与后台数据的交互,页面设计灵活,不用管后台如何写的,前台与后台的交互唯一的交互通道都是xml,在我们需要添加页面.添加规定的格式的 ...

  8. Java数组与函数的结合

    import java.util.Scanner; public class HelloWorld { public static void main(String[] args){ // Scann ...

  9. 产品打包工具的制作,ant,编译源码,打jar包,打tag,打war包,备份release版本等

    1.  在进行打包工具的制作前,需要准备的软件有: svnant-1.3.1 作用是让ant和svn相关联 apache-ant-1.9.7 需要设置ant_home,path,我的配置是: ANT_ ...

  10. 用过的一些Android设备调试特性注意点(挖坑帖)

    华为3C Activity切换动画偏快. 显示大图时不容易出现OOM(应用最大内容要比其他手机大一点),所以调试OOM问题时不要用此手机,否则难以发现问题. 小米3 不要调用系统的裁图功能.因为返回的 ...