Given an integer n, return the number of trailing zeroes in n!.
Example 1:
Input: 3
Output: 0
Explanation:
3! = 6, no trailing zero. Example 2:
Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Note:
Your solution should be in logarithmic time complexity. 注意:您的解决方案应该是对数时间复杂度。
思路: 只要有一个2和5就会有一个零 看这个数有多少2和5
此方法出错
class Solution(object):
def trailingZeroes(self, n):
"""
:type n: int
:rtype: int
"""
i = 1
j = 1
while 2*i <= n or 5*i <= n:
if 5*j <= n:
print(5*i)
print("5的个数", j)
j += 1
elif 2*i <= n:
print(2*i)
print("2的个数", i)
i += 1
return min(i-1, j-1)
print(min(i-1, j-1))
if __name__ == "__main__":
Solution().trailingZeroes(30)
print(Solution().trailingZeroes(6))
正解:
class Solution(object):
def trailingZeroes(self, n):
"""
:type n: int
:rtype: int
"""
i = 0
while n // 5 > 0:
i += n // 5
n = n // 5
#print(i)
return i
if __name__ == "__main__":
Solution().trailingZeroes(30)
print(Solution().trailingZeroes(135))
网上思路: https://blog.csdn.net/coder_orz/article/details/51590478