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, 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         b=str(b)
11
12         for i in range(len(b)-1,-1,-1):
13             if int(b[i])>0:
14                 return len(b)-1-i

通过:

数因子里面5的个数:

1 class Solution:
2     def trailingZeroes(self, n: int) -> int:
3         if n<5:
4             return 0
5         count=0
6         while n>=5:
7             count=count+n//5
8             n=n//5
9         return count

执行用时 :72 ms, 在所有 Python3 提交中击败了17.11%的用户

内存消耗 :13.7 MB, 在所有 Python3 提交中击败了5.19%的用户

——2019.9.24

原文地址:https://www.cnblogs.com/taoyuxin/p/11578488.html

时间: 2024-08-30 12:43:06

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

最开始一看,就觉得挺简单,就是先算出阶乘的值,再除以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

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!