【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日 ...
随机推荐
- 分布式任务队列 Celery —— 深入 Task
目录 目录 前文列表 前言 Task 的实例化 任务的名字 任务的绑定 任务的重试 任务的请求上下文 任务的继承 前文列表 分布式任务队列 Celery 分布式任务队列 Celery -- 详解工作流 ...
- Python基础(函数部分)
写在前面 加勒比海盗今天上映! 一.函数的基本概念 - 函数是什么? 函数,就是一个'锤子',一个具有特定功能的'锤子',使用者可以在适当的时候使用这个'锤子',而不用再去从头做一个'锤子':即可以 ...
- python学习之文件读写操作
open函数 在使用文件之前,需要先打开,即使用open函数 如: files=open("文件路径","操作方式") 解释如下: (1.files:为文件对象 ...
- H5如何测试?
它跟安卓APP与IOS APP有什么样的区别呢?★ 我们以往的APP是使用原生系统内核的,相当于直接在系统上操作,是我们传统意义上的软件,更加稳定 ★ H5的APP先得调用系统的浏览器内核,相当于是在 ...
- 一个ETF基金经理的心路历程
简介: 鹏华沪深300ETF拟任基金经理崔俊杰先生,金融工程专业管理学硕士,5年证券基金从业经验.2008年7月加盟鹏华基金管理有限公司,历任产品规划部产品设计师.量化投资部量化研究员,先后从事产品设 ...
- 一图看懂python对excel的读写
- 【MM系列】SAP 创建工厂
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 创建工厂 前言部分 大家 ...
- 用WebService实现对数据库进行操作(添加+删除+修改)(转)
转自:http://blog.csdn.net/beyondqd/article/details/6703169 表为User,字段有 编号: int id,用户名:string UserName,密 ...
- CSS中BFC规则
何为BFC BFC(Block formatting context)直译为"块级格式化上下文".它是一个独立的渲染区域,只有Block-level box参与, 它规定了内部的B ...
- 【五一qbxt】day3 动态规划
动态规划 引例: 斐波那契数列: 边界条件:f0=0: f1=1: 能够直接被求出值的状态 不需要计算其他斐波那契数列的值直接可以得到结果: 转移方程:fn=fn-1+fn-2如何用已有状态求出未知状 ...