题目:

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. 如何优雅地退出python程序

    如何优雅地退出python程序 一个单模的python程序,启动之后要能够优雅地关闭.即当用户按Ctrl+C或者kill pid的时候,程序都能从容关闭.实现起来非常简单. is_running = ...

  2. ROS(indigo)MoveIt!控制ABB RobotStudio 5.6x 6.0x中机器人运动

    Gazebo以及相关参考文献,参考: ROS(indigo)ABB机器人MoveIt例子 这里需要配置RobotStudio,请参考ROS官网教程.下面列出要点:   window端配置结束后,在Ub ...

  3. 15 Action View 以及监听 的使用

    menu 代码 <menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- a ...

  4. Android旋转动画

    Android旋转动画 核心方法 public void startAnimation(Animation animation) 执行动画,参数可以是各种动画的对象,Animation的多态,也可以是 ...

  5. Android初级教程通过简要分析“土司”源码,来自实现定义土司理论探讨

    由于系统自带的土司瞬间即逝,而且非常难看.因此我们就希望自定义自己的土司风格.有些实例就是基于自定义土司完成的,例如金山卫士的火箭发射,基本原理就是个土司.但是在做出自己的土司风格之前,还是要简要分析 ...

  6. UNIX网络编程——TCP服务器“拒绝服务攻击” 解决方案

    前面的博客<<使用select和shutdown>>里面的拒绝服务型攻击也有提到. 说这是一个完全的解决方案,其实有点夸大了,但这个方案确实可以缓解TCP服务器遭受" ...

  7. Android Studio(AS)-->导入项目

    1:首先,你必须要有一个工程(Project), 才可以打开项目(Module); (注意:Eclipse中的Workspace对应Android Studio 中的Project, Eclipse中 ...

  8. 如何在Cocos2D游戏中实现A*寻路算法(五)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  9. Checksum软件的简单设计

    相信大家平时在测试一些bin文件的时候,经常都会要求计算checksum值,其实就是校验和,非常的简单,比如下图这个软件:

  10. Java-IO之对象输入流输出流(ObjectInputStream和ObjectOutputStream)

    ObjectInputStream和ObjectOutputStream的作用是对基本数据和对象进行序列化操作支持.创建文件输出流对应的ObjectOutputStream对象,该ObjectOutp ...