有关阶乘的两个问题1
有关阶乘的两个问题1
http://www.nowcoder.com/questionTerminal/aa03dff18376454c9d2e359163bf44b8
阶乘结果的0和乘数的2和5有关,而2的个数远多于5,所以只要数5。而5,25,125的倍数是自相似的,所以可以用递归。时间复杂度O(logN)。
参考:https://blog.csdn.net/qq_36705705/article/details/106890761
用例通过率: 100.00% 运行时间: 32ms 占用内存: 6520KB。
import math
#
# the number of 0
# @param n long长整型 the number
# @return long长整型
#
class Solution:
def thenumberof0(self, n):
return recurve(n)
def recurve(n):
if n < 5:
return 0
else:
return n//5+recurve(n//5)
文远知行公司福利 544人发布