【leetcode】Factorial Trailing Zeroes
题目描述:
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
解题思路:
这个题目给的评级是easy,其实只要想到要求n!中0的个数,能够得到0的只有:2,4,5,10,100.。。。而这里面又以5最为稀缺,所以说我们可以得出阶乘的最终结果中的0的数量等于因子中5的数量,比如说10,阶乘含两个0,因为有5,10;30含7个0,因为有5,10,15,20,25(25=5*5),30。
那么怎么求出有多少个呢?
简单的想法是:令\(f(n)\)表示\(n!\)有多少个0
\]
\]
递归版本:
class Solution:
# @return an integer
def trailingZeroes(self, n):
if n < 5:
return 0
else:
return n/5 + self.trailingZeroes(n/5)
迭代版本:
class Solution:
# @return an integer
def trailingZeroes(self, n):
counter = 0
while n:
counter += n / 5
n /= 5
return counter
【leetcode】Factorial Trailing Zeroes的更多相关文章
- 【leetcode】Factorial Trailing Zeroes(easy)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes
题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Java for LeetCode 172 Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- leetcode:Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. 最初的代码 class Solution { public: int t ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
随机推荐
- 打电话,发短信,发邮件,app跳转
1.打电话 - (IBAction)callPhone1:(id)sender { NSURL *url = [NSURL URLWithString:@"tel://18500441739 ...
- java 对象入门
对象的五大特征 (1)所有东西都是对象.可将对象想象成一种新型变量;它保存着数据,但可要求对自身进行操作.理论上讲, 可从要解决的问题身上提出所有概念性的组件,然后再程序中将其表达为一个对象. (2) ...
- NFS网络文件共享
NFS(Network File System) NFS在企业中的应用场景 企业集群架构中,NFS网络文件系统一般用来存储共享的视频.图片.附件等静态资源,一般把网站用户上传的文件都放到NFS共享里, ...
- C语言基础(3)-二进制、八进制、十六进制
1.二进制 一个位只能表示0或者1两种状态,简称bit(比特) 一个字节为8个二进制数,称为8位,简称BYTE(字节) 一个字为2个字节,简称WORD. 两个字为双字,简称DWORD,占32个bit ...
- AngularJS常用插件与指令收集
angularjs 组件列表 bindonce UI-Router Angular Tree angular-ngSanitize模块-$sanitize服务详解 使用 AngularJS 开发一个大 ...
- Dijkstra算法
Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. /*图的邻接矩阵表示*/typedef ...
- 大熊君JavaScript插件化开发------(实战篇之DXJ UI ------ Tab功能扩展完结版)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得上一篇文章吗.主要讲述了一个“Tab”插件是如何组织代码以及实现的”,以及过程化设计与面向对象思想设计相结合的方式是 如何设计一个插件的,两种方 ...
- JavaScript闭包(Closure)学习笔记
闭包(closure)是JavaScript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面就是我的学习笔记,对于JavaScript初学者应该是很有用的. 一.变量的作用域 要理解 ...
- 第2月第24天 coretext 行高
1.NSMutableAttributedString 行高 NSMutableAttributedString *attributedString = [[NSMutableAttributedSt ...
- Node.js与Express4安装与配置
Nodejs简介 Node.js 基于 Chrome JavaScript 运行环境,用于便捷地搭建快速.可扩展的网络应用. 它使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效, 非常适合 ...