Factorial Trailing Zeroes 172

题目描述:

给出一个integer n,计算n!结尾0的个数


题目分析:

考虑暴力,计算n!统计最后面0的个数。先不说数字溢出,其次n是一个integer ,O(n)复杂度超时

我们接着考虑,产生0的情况

只有包含因子5的数乘以一个偶数会在结尾产生0(5*2,15*2,75*2),因为偶数的个数大于因子包含5的数的个数,那么我们只要考虑包含因子5的数;

但是是不是包含因子5的数一定只会在结尾产生一个0呢,对于25*4提供两个0,这是因为25,包含两个因子5(5*5);同样125会提供3个0;

这样只要考虑1-n之间有多少包含因子5的数,有多少个包含因子25的数,有道少个包含因子125的数。。。。以此累加


代码:

1 int trailingZeroes(int n) {
2         int mod=5;
3         int cnt=0;
4         while(n/mod){
5             cnt+=n/mod;
6             mod*=5;
7         }
8         return cnt;
9     }
时间: 2024-11-10 12:03:56

Factorial Trailing Zeroes 172的相关文章

Factorial Trailing Zeroes , 172题,可能是我没读清楚,还报错。

package com.x.xyz; public class HelloWorld { public static void main(String[] args) { System.out.println("Star !"); System.out.println(trailingZeroes(0)); System.out.println(trailingZeroes(1)); System.out.println(trailingZeroes(2)); System.out.p

[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相乘得来的.如果我们可以计数

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

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】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*

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的

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)