Java-Factorial Trailing Zeroes

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

Note: Your solution should be in logarithmic time complexity.

Credits:

Special thanks to @ts for adding this problem and creating all test cases.

看了半天没看懂题目意思 以为要求n结尾0的个数 搞了半天是要求n!即 n的阶乘结果中结尾有多少0   可以知道当阶乘因子中有一对5和2的倍数即可产生0 而对于阶乘来讲 5的个数少于2的个数 即有5即有2与其搭配得到0 所以只要计算 有多少5的倍数即可 对于像25 125这样的数可以分解为 2个3个 5*5 所以对于得到的结果还要进行除五以得到像5^n这样的数中多的5  一直循环至n=0 代码如下:

public class Solution {
    public int trailingZeroes(int n) {
        int count=0;
        while(n>0){
            count+=n/5;
            n=n/5;
        }
        return count;
    }
}
时间: 2024-08-01 10:46:34

Java-Factorial Trailing Zeroes的相关文章

LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)

172. 阶乘后的零 LeetCode172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! = 120, 尾数中有 1 个零. 说明: 你算法的时间复杂度应为 O(log n) . Java 实现 class Solution { // 递归思路 public static int trailingZe

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!. Note: Your solution should be in logarithmic time complexity. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

[LeetCode]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. 分析 朴素解法: 首先求出n!,然后计算末尾0的个数.(重复÷10,直到余数非0) 该解法在输入的数字稍大时就会导致阶乘得数溢出,不足取. O(logn)解法: 考虑n!的质数因子. 后缀0总是由质因子2和质因子5相乘得来的.如果我们可以计数

Factorial Trailing Zeroes

Factorial Trailing Zeroes Total Accepted: 44612 Total Submissions: 144778 Difficulty: Easy Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (M) Number of Digit One n!的结果中0的个

LeetCode Day4——Factorial Trailing Zeroes

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出现的

【LeetCode】Factorial Trailing Zeroes (2 solutions)

Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. 对n!做质因数分解n!=2x*

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 logarithmic time complexity. 解题思路: 计算n能达到的5的最大次幂,算出在这种情况下能提供的5的个数,然后减去之后递归即可,JAVA实现如下: static public int trailingZeroes(int n) { if(n<25) return n/5; lon

Java [Leetcode 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. 解题思路: 对于阶乘而言,也就是1*2*3*...*n[n/k]代表1~n中能被k整除的个数那么很显然[n/2] > [n/5] (左边是逢2增1,右边是逢5增1)[n/2^2] > [n/5^2](左边是逢4增1,右边是逢25增1)

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 logarithmic time complexity. 分析 Note中提示让用对数的时间复杂度求解,那么如果粗暴的算出N的阶乘然后看末尾0的个数是不可能的. 所以仔细分析,N! = 1 * 2 * 3 * ... * N 而末尾0的个数只与这些乘数中5和2的个数有关,因为每出现一对5和2就会产生

172. Factorial Trailing Zeroes

1. 问题描述 Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.Tags: MathSimilar Problems: (H) Number of Digit One 2. 解题思路 分解质因子, 当且仅当 因子中出现 一对 (2,5)时, 最后结果会增加一个 trailing zero.1. 2的