Leetcode#172 Fractorial Trailing Zero

原题地址

n!含有多少个因子10,则结尾有多少个0

10=2*5,而2的个数肯定比5多,所以n!含有多少个因子5,则结尾有多少个0

如何计算n!有多少个因子5呢?

比如n=13,则:

n! = 13 * 12 * 11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1                    |                    |含有因子5:           10                   5

实际上我们就是在找1到n里面能被5整除的数字有多少个。

显然每隔5个数就有一个带因子5的数字出现,所以就是n/5嘛,错!这样还是少算了一些数,比如25=5*5,丫可是包含2个5的,所以还要加上那些每隔25个数、125个数、...出现的数字(5^i)。

代码:

1 int trailingZeroes(int n) {
2   if (n < 0) return -1;
3   int res = 0;
4   while (n > 0)
5     res += (n /= 5);
6   return res;
7 }

《Cracking the Code》里面也有这道题,这是书上的代码:

1 int trailingZeroes(int n) {
2   if (n < 0) return 0;
3   int count = 0;
4   for (int i = 5; n / i > 0; i *= 5)
5     count += n / i;
6   return count;
7 }

这个代码是无法通过Leetcode测试的,因为i *= 5会溢出,而前面的代码不会。

时间: 2024-08-29 23:53:42

Leetcode#172 Fractorial Trailing Zero的相关文章

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

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

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

翻译 给定一个整型n,返回n!后面的零的个数. 注意:你的解决方案应该在log时间复杂度内. 原文 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 起初我看题目的时候没太注意,还以为就是求n这个数后面的零而已,虽然心想不会这么简单吧--就写了一份代码提交了,结果WA提示我5的话应该返回1,

leetcode——172 Factorial Trailing Zeroes(N!尾巴上有多少个0,算法复杂度为lg)

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity.(您的解决方案应该在对数时间复杂度.) Hide Tags: Math 题目要求:给定N,求N!的末尾有多少0.要求算法复杂度为lg 解题思路: 思路一: 想的比较简单,先实用for循环进行阶乘运算,然后mod10计算0的个数,但是在OJ检查时,超时

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就会产生

[LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数

所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125....直到n/5商为0 return n==0?0:n/5+trailingZeroes(n/5); 原文地址:https://www.cnblogs.com/stAr-1/p/8477839.html

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