LeetCode172——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,返回n!的末尾0的个数,要求对数阶时间复杂度。

乍一看,这确实不难,很简单的做法是先求n!嘛,然后求0的个数。结果是没问题,但是时间复杂度不符合要求,求n!就要O(n)的时间复杂度。

虽然说那样做不符合要求,但你稍微思考下,也轻松发现有更好的办法。0的个数跟有多少个10相乘有关,而要产生10,分解下就得有2和5,很容易发现,阶乘过程中,能分解成5的数一定比能分解成2的数要少。因此,就看n!能有多少个数可以分解成5的。

我当时就是这么想的,5!有1个5,10!有2个5,于是我就得出:

int trailingZeroes(int n) {
    return n / 5;
}

然后我就傻傻地提交了,结果当然就做错了。并且告诉我输入30,应该是7,而我的结果是6。

我就思考,为什么会多出个0呢,原来25 * 4 就可以得到2个0。即25可以分解为两个5,因此,当n/5 >= 5时,还应该继续除下去。于是改成这样:

int trailingZeroes(int n) {
    if (n / 5 < 5) {
        return n / 5;
    } else {
        return n /5 + trailingZeroes(n / 5);
    }
}

我再次提交,这回就正确了。

好吧,我老实地写出来,我是有压力的。因为这题并不难,我却第一次做错了。只要写完简单地验证下,也可以发现错误。事实上,我是有验证的,我写了个求阶乘的函数,参数类型是long,蛋疼的是,到25!的时候,溢出了。这就让我看不到结果有几个0,我当时认为前面几个数验证都对了,应该没问题了吧,我想。就这样,第一次提交出错了。

这题就讲解到这里了。以前没刷过算法题,之所以现在开始刷,是因为我感觉自己练的不够多,编码能力不强,顺便掌握写常用的算法。当我不知道自己做什么的时候,我就去刷题,并把每一道题都记录成blog,代码全部用C++来写。

时间: 2024-10-16 16:26:47

LeetCode172——Factorial Trailing Zeroes的相关文章

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. 此题是求阶乘后面零的个数. public class Solution { public int trailingZeroes(int n) { int t=0; while(n!=0){ n/=5; t+=n; } return t; } }

[Leetcode172]Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. solution: zero comes from 2*5, and number of 2 is less than 5. So we can only count the number of 5 contained in n!. public

LeetCode----172. Factorial Trailing Zeroes(Java)

1 package singlenumber136; 2 //Given an array of integers, every element appears twice except for one. Find that single one. 3 //Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 4 public class

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

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