【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_05.使用Mybatis完成CRUD_7 Mybatis中参数的深入-使用实体类的包装对象作为查询条件
pojo对象就是实体类 综合查询-实体类包装起来做查询 新建实体类QueryVo 提供一个User对象属性,并生成getter和setter 测试 修改dao接口中的返回类型为List<User ...
- 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_07.环境搭建的注意事项
2 resources下面创建目录要一级一级的创建,下面这个创建的就是一级目录而不是三级 在文件夹下看到的目录也是一级的 因此这里创建目录需要一个个的去创建 配置文件和dao类这两个目录要保持一致,这 ...
- idea中@data不生效
idea中@data不生效,原因是idea中没有安装插件,记得重启
- 读取yaml中的内容
def read_yml(path): """ 读取yml文件中的数据 :param path: 文件yaml 的路径 :return: 返回读取yaml文件内的结果 & ...
- 正则表达式——Unicode
第 7 章 Unicode 7.1 关于编码 通常,英文编码较为统一,都采用ASCII编码或可以兼容ASCII编码(即编码表的前127位与ASCII编码一直,常见的各种编码,包括Unicode编码 ...
- JDK8新特性之一Lambda
JDK8的新特性之一Lambda能将函数作为方法里面的参数使用. /** * JDK8新特性Lambda */ public class Test { public static void main( ...
- 【BZOJ2622】[2012国家集训队测试]深入虎穴
虎是中国传统文化中一个独特的意象.我们既会把老虎的形象用到喜庆的节日装饰画上,也可能把它视作一种邪恶的可怕的动物,例如“武松打虎”或者“三人成虎”.“不入虎穴焉得虎子”是一个对虎的威猛的形象的极好体现 ...
- Java——LinkedHashMap源码解析
以下针对JDK 1.8版本中的LinkedHashMap进行分析. 对于HashMap的源码解析,可阅读Java--HashMap源码解析 概述 哈希表和链表基于Map接口的实现,其具有可预测的迭 ...
- java8--- Optional的使用
// https://www.jianshu.com/p/82ed16613072 1.Optional.of(T value),传入非 null(否则会抛出 NullPointerException ...
- Django创建mysql数据表流程
在Django项目建好后,在setting.py中设置好mysql连接参数: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysq ...