【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日 ...
随机推荐
- CentOS6.5 编译安装Nginx
一.准备编译环境 1. 系统为centos6.5最小安装,关闭selinux,关闭防火墙. 2. 安装编译工具 # yum -y install gcc gcc-c++ autoconf automa ...
- golang error (slice of unaddressable value)
使用 Golang 将生成的 md5 转化为 string 的过程出现如下编译错误: 错误解析: 值得注意的一点是 func Sum(data []byte) [Size]byte 这个函数返回的 ...
- Golang基础(4):Go结构体
当我们要表示同一种数据类型时候,可以用到数组,切片和字典. 当我们要表示不同的数据类型呢?这时候就要用到结构体了 一:定义struct 关键字 type 和 struct 来定义结构体 type st ...
- Fabric CA/数字证书管理
MSP(Membership Service Provider)成员管理服务提供商 名词: 1.CSR(Cerificate Signing Request):证书签署请求文件 CSR里包含申请者的 ...
- C语言I-2019博客作业02
这个作业属于哪个课程 C语言程序设计I 这个作业要求在哪里 C语言I-2019秋作业02 我在这个课程的目标是 学会编程及提问的技能 这个作业在哪个具体目标方面帮助我实现目标 深入了解C语言程序设计中 ...
- Linux cat 多行写入文件防止变量替换
Linux cat 多行写入文件防止变量替换 问题描述 对多个变量及多行输出到文件,存在变量自动替换,当使用cat<<EOF不想对内容进行变量替换.命令替换.参数展开等 问题解决 转义特 ...
- P3951小凯的疑惑
这是2017年提高组的第一题,是一个小学奥数题?听说很多大佬爆零了,我AC了,,, 这个题首先给出两个素数,问取任意个这两个素数之和不可以达到的最大的数是多少?拿到这个题首先很蒙,于是试了试样例,并没 ...
- 关于微信H5页面开发中音乐不自动播放的解决方法
我想应该有很多人在做H5场景应用.H5微刊.H5微杂志的时候加入背景音乐吧(客户需求),相信很多人一定碰过不能自动播放的时候,即使是相同的iPhone 5s也有不播放的时候,很蛋疼吧!? 之前我的解决 ...
- Jafka源码分析——网络架构
在kafka中.每个broker都是一个server.依照一般理解,server就是一个SocketServer,其不断接收用户的请求并进行处理.在Java中进行网络连接有两种方式一种为堵塞模式一种为 ...
- java 技术分享
http://www.ccblog.cn/99.htm http://www.ccblog.cn/100.htm http://www.ccblog.cn/101.htm http://www.ccb ...