【leetcode】Preimage Size of Factorial Zeroes Function
题目如下:

解题思路:《编程之美》中有一个章节是不要被阶乘吓倒,里面讲述了“问题一:给定一个整数N,那么N的阶乘末尾有多少个0呢?例如N = 10, N! = 362800,N! 的末尾有两个0.” 这个问题的解法。本题就是在这个问题的基础上把输入和输出倒过来了。首先,对于输入参数K,我们可以知道输出结果的范围是[0,K*5],同时我们也可以知道,对于任意两个正整数i,j (i>j),i的阶乘一定是大于j的阶乘的,那么i的阶乘末尾0的数量也一定是大于或者j的阶乘末尾0的数量,既然是一个单调递增的关系,那么就可以采用二分查找来判断输入参数K存不存在。最后,题目要求的返回值是满足阶乘值末尾0的数量等于K的数字的个数,这也很简单,因为我们从《编程之美》的案例中可以知道,阶乘值每多一个5,0的数量就会变化。因此,如果K存在,那么满足条件的数字就是5,如果不存在则是0。
代码如下:
class Solution(object):
def calcFactorial(self,v):
n = v
count = 0
while n > 0:
count += n / 5
n = n / 5
return count
def preimageSizeFZF(self, K):
high = K*5
low = 0
while low <= high:
mid = (high + low)/2
if self.calcFactorial(mid) < K:
low = mid +1
elif self.calcFactorial(mid) > K:
high = mid - 1
else:
return 5
return 0
【leetcode】Preimage Size of Factorial Zeroes Function的更多相关文章
- 793. Preimage Size of Factorial Zeroes Function
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...
- [LeetCode] Preimage Size of Factorial Zeroes Function 阶乘零的原像个数函数
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...
- 74th LeetCode Weekly Contest Preimage Size of Factorial Zeroes Function
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...
- [Swift]LeetCode793. 阶乘函数后K个零 | Preimage Size of Factorial Zeroes Function
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...
- 【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【LeetCode】二分 binary_search(共58题)
[4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
随机推荐
- 阶段3 1.Mybatis_03.自定义Mybatis框架_6.自定义Mybatis的编码-实现基于XML的查询所有操作
接下来就可以写创建代理对象的方法了 类加载器,代理谁,就用谁的加载器,因为这里用daoInterfaceClass.getClassLoader() 第二个代理谁就要和谁有相同的接口,daoInter ...
- OutLook会议室预定提醒
项目组采用敏捷开发管理,每两周一个迭代.写个工具做会议室预定. 代码下载:https://download.csdn.net/download/linmilove/10547579 Appointme ...
- expect实战
1.测试主机是否在线 #!/bin/bash#创建一个IP地址文件.>ip.txt (清空文本)#使用for循环ping测试主机是否在线.for i in {2..254}do ...
- JS图片宽度自适应移动端
$(function(){ $("#d-intro").find("img").each(function () { $(this ...
- django学习——通过HttpResponseRedirect 和 reverse实现重定向(转载)
人分类: django 用django开发web应用, 经常会遇到从一个旧的url转向一个新的url,也就是重定向. HttpResponseRedirect:构造函数的第一个参数是必要的 — 用 ...
- Nginx服务器优势是什么
nginx介绍.功能,优势 https://www.cnblogs.com/wcwnina/p/8728391.html#!comments Nginx负载均衡,session共享问题,几种解决方案 ...
- Python 内置函数super
super()函数是用于调用父类/超类的一个方法 super是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没有问题,但是如果使用多继承,会涉及到查找顺序(MRO),重复调用(钻石继 ...
- RBAC----基于角色的访问权限控制
RBAC是什么? 基于角色的权限访问控制(Role-Based Access Control) 作为传统访问控制(自主访问.强制访问)的有前景的代替 受到了广泛的关注. 在RBAC中,权限与角色相关联 ...
- 正反向代理、负载均衡、Nginx配置实现
一.正反向代理 1.前提 我们曾经使用翻墙软件,访问google:使用了代理软件时,需要在浏览器选项中配置代理的地址,我们仅仅有代理这个概念,并不清楚代理还有正向和反向之分. 2.正向代理(代替客户端 ...
- Vim实用技巧(一)
vim 命令按键规定 标记 含义 x 按一次 x dw 按一次 d, w dap 按一次 d, a, p 同时按 和 n g<C-]> 按 g, 然后同时按 和 ] <C-=> ...