leetcode 172. Factorial Trailing Zeroes

来源:互联网 发布:mysql排错指南 pdf 编辑:程序博客网 时间:2024/06/06 20:36

Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.

思路:要求n!中有多少个0
10的因子有2,5,5即解题关键(因为有5必有2)
2*5=10有一个0
10也有一个0
因此在一个[1,10]区间内有2个0
又因为,25有两个5,125有三个5,它们的倍数会比多一个0

class Solution(object):    def trailingZeroes(self, n):        """        :type n: int        :rtype: int        """        count=0        tmp=5        while(n/tmp):            count=count+n/tmp            tmp=tmp*5        return count
0 0
原创粉丝点击