【leetcode 简单】第四十一题 阶乘后的零

给定一个整数 n,返回 n! 结果尾数中零的数量。

示例 1:

输入: 3
输出: 0
解释: 3! = 6, 尾数中没有零。

示例 2:

输入: 5
输出: 1
解释: 5! = 120, 尾数中有 1 个零.

说明: 你算法的时间复杂度应为 O(log n) 

class Solution(object):
    def trailingZeroes(self, n):
        """
        :type n: int
        :rtype: int
        """
        count = 0
        while n:
            n //= 5
            count += n
        return count

原文地址:https://www.cnblogs.com/flashBoxer/p/9508936.html

时间: 2024-07-31 23:03:18

【leetcode 简单】第四十一题 阶乘后的零的相关文章

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

light_oj 1138 求阶乘后导零的个数

light_oj 1138  求阶乘后导零的个数 N - Trailing Zeroes (III) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1138 Description You task is to find minimal natural number N, so that N! contains exactly Q 

【每天一题】LeetCode 172. 阶乘后的零

开源地址:点击该链接 题目描述 https://leetcode-cn.com/problems/factorial-trailing-zeroes 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! = 120, 尾数中有 1 个零. 说明: 你算法的时间复杂度应为O(logn). 解题思路 最直接的解法就是先求出 n! 等于多少 然后计算尾数中零的数量,该方法的复杂度

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. 阶乘后的零】

最开始一看,就觉得挺简单,就是先算出阶乘的值,再除以10,如果%为0,count++,然后s=s/10,如果不为0,就直接输出. class Solution { public int trailingZeroes(int n) { int i; int s = 1; int count = 0; for(i = 1;i <= n;i++) { s = s*i; } while(s != 0) { if(s % 10 == 0) { count++; s = s / 10; } else { b

[C++]LeetCode: 88 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. 我们来找一下规律,考虑n!的质数因子.后缀0,只有可能是质因子2 * 质因子5得到.如果我们可以计算得到min{num(2), num(5)},就可以知道后导0的个数. 举例子: n = 5!

LeetCode 172. 阶乘后的零

给定一个整数 n,返回 n! 结果尾数中零的数量. 示例 1: 输入: 3输出: 0解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5输出: 1解释: 5! = 120, 尾数中有 1 个零. 说明: 你算法的时间复杂度应为 O(log n) .算法:我们只需要考虑n!中有多少个5即可. class Solution { public: int trailingZeroes(int n) { return n<5?0:n/5+trailingZeroes(n/5); } }; 原文地

leetcode——172.阶乘后的零

超时: 1 class Solution: 2 def trailingZeroes(self, n: int) -> int: 3 if n<5: 4 return 0 5 b=n 6 i=n 7 while i>1: 8 i=i-1 9 b=b*i 10 11 j=0 12 t=10 13 while b%t==0: 14 j=j+1 15 t=t*10 16 return j 再次超出时间限制: 1 class Solution: 2 def trailingZeroes(self

172 Factorial Trailing Zeroes 阶乘后的零

给定一个整数 n,返回 n! 结果尾数中零的数量.注意: 你的解决方案应为对数时间复杂度. 详见:https://leetcode.com/problems/factorial-trailing-zeroes/description/ class Solution { public: int trailingZeroes(int n) { int res=0; while(n) { n/=5; res+=n; } return res; } }; 参考:https://www.cnblogs.c