码迷,mamicode.com
首页 > 其他好文 > 详细

29.leetcode172_factorial_trailing_zeroes

时间:2018-02-14 21:05:23      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:leetcode   note   time   log   线性时间   number   ret   class   pre   

1.题目描述

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

给一个数n,在线性时间内得出末尾0的个数

2.题目分析

因为0的个数与10的倍数相等,10可以分解为2和5,所以只要求出n范围内的可以整除2和5的数的个数,取较少的那个。因为5>2,所以5的会比较少,因此只求出能整除5的个数的树就可以了。但应该注意像n=5**i(i>1)这种情况

3.解题思路

 1 class Solution(object):
 2     def trailingZeroes(self, n):
 3         """
 4         :type n: int
 5         :rtype: int
 6         """
 7         if n/5<5:   #如果n/5>=5的话,说明还能再次分解一下
 8             return n/5
 9         else:
10             return self.trailingZeroes(n/5)+n/5

 

29.leetcode172_factorial_trailing_zeroes

标签:leetcode   note   time   log   线性时间   number   ret   class   pre   

原文地址:https://www.cnblogs.com/19991201xiao/p/8448834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!