LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
题目意思:
n求阶乘以后,其中有多少个数字是以0结尾的。
方法一:
class Solution:
# @return an integer
def trailingZeroes(self, n):
res = 0
if n < 5:
return 0
else:
return n/5+ self.trailingZeroes(n/5)
方法二:
class Solution:
# @return an integer
def trailingZeroes(self, n):
res = 0
x = 5
while( n >= x):
res += n/x
x *= 5
return res
参考博文:http://www.tuicool.com/articles/RZZnQf
n!后缀0的个数 = n!质因子中5的个数 = floor(n/5) + floor(n/25) + floor(n/125) + ....
LeetCode Factorial Trailing Zeroes Python的更多相关文章
- [LeetCode] 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
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...
- 关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解
题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0 ...
- [LeetCode] Factorial Trailing Zeroes 阶乘末尾0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Python3解leetcode Factorial Trailing Zeroes
问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 ...
- LeetCode Factorial Trailing Zeroes (阶乘后缀零)
题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
随机推荐
- Netty优雅退出机制和原理
1.进程的优雅退出 1.1.Kill -9 PID带来的问题 在Linux上通常会通过kill -9 pid的方式强制将某个进程杀掉,这种方式简单高效,因此很多程序的停止脚本经常会选择使用kill - ...
- Eclipse 中 SDK无法更新---解决方法
在SDK Manager -> tools -> options中: HTTP Proxy Server: mirrors.neusoft.edu.cn HTTP Proxy Port: ...
- LeetCode--202--快乐数
问题描述: 编写一个算法来判断一个数是不是“快乐数”. 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变 ...
- python-day70--django-Mysql-单表增删改查
项目名:bookmanage app01文件夹 内的 __init__.py import pymysql pymysql.install_as_MySQLdb() app01文件夹 内的models ...
- Error: [ng:areq] Argument 'LoginCtrl' is not a function, got undefined
- OC NSArray数组排序
一.一般排序 // 排序 NSArray *arr = @["]; NSArray *newarr = [arr sortedArrayUsingSelector:@selector(com ...
- HTML绘制三角形的方法:
<!DOCTYPE html> <html> <body> <style> #triangle-up { width: 0px; height: 0px ...
- gitignore中常见需要被无视的文件
gitignore中常见的需要被忽略的文件:例如各个系统.一些软件会自动生成的文件,主要适用于web项目. 复制后,保存进.gitignore文件中即可. # Project node_modules ...
- OPENVZ低版本centos6.5安装BBR加速手记
玩 VPS,开机第一件事就是安装 BBR,至于效果怎么样还真不好说,依据不同的线路质量而定,但有总比没有好. 因为这次用的是 openvz 平台,所以找了一个网上的 ovz 专用的 BBR 一键安装代 ...
- SharePoint 2010 Ribbon with wrong style in Chrome and Safari
When we add custom ribbon to SharePoint 2010, it may display well in IE but not in Chrome and Safari ...