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
            {
                break;
            }
        }
        return count;
    }
}

前面都很正常,有问题的就是13以后,因为int型是4个字符,所以超过了就会自动省略后面的值,

s改为long型时,就是到30以后,有问题,那么,计算出来阶乘值这个思路是有问题的,数值太大了。

再思考思考10,可以考虑其中有多少个5.

class Solution {
    public int trailingZeroes(int n) {
        if (n <= 1) {
            return 0;
        }
        int count = 0;
        while (n != 0) {
            count = count + n / 5;
            n = n / 5;
        }
        return count;
    }
}

这里之所以n = n/5,因为每隔5个数,应该多了一个5,

比如30

应该有30,25,20,15,10,5,其中,25就含有两个5,

同时,50,45,40,35,30...,50含有两个5,就是每隔5个数,count应该再增加1

那么在这里n/5就可以判断是否大于5个数。

原文地址:https://www.cnblogs.com/wzwi/p/10888871.html

时间: 2024-08-30 05:02:48

LeetCode【172. 阶乘后的零】的相关文章

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

给定一个整数 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. 阶乘后的零

开源地址:点击该链接 题目描述 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.阶乘后的零

超时: 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

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 

172.阶乘后的0

2020-03-18 阶乘后的零 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有0 输入: 5 输出: 1 解释: 5! = 120, 尾数中有一个0 解释: L D R E O E I I E C I H N T S G --> 说明: 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串. --> 题解: 思路1: 数学归纳 尾数为0就是2x5或者是10,分析可知每5个数有一个

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,

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

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