leetcode-172-Factorial Trailing Zeroes
2015-08-31 19:25
507 查看
Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
求 n!中,末尾的0连续有多少个。
例如 10!=10*9*8*7*6*5*4*3*2*1 所以末尾0的个数为2
因为,n!可以写成质因数的乘积,n!=2x*3y*5z*......
,2*5=10, 故末尾0的个数等于min(x,z),即2与5的个数的最小值。
5的个数比2的个数大,因为n!中,每两个数就含有2,每5个数才含有5。
所以只用求n!的质因数成绩中,5的个数即可。
n/5得到5个数,但像25,75.....这种有多个5的,就需要再n/5
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
求 n!中,末尾的0连续有多少个。
例如 10!=10*9*8*7*6*5*4*3*2*1 所以末尾0的个数为2
因为,n!可以写成质因数的乘积,n!=2x*3y*5z*......
,2*5=10, 故末尾0的个数等于min(x,z),即2与5的个数的最小值。
5的个数比2的个数大,因为n!中,每两个数就含有2,每5个数才含有5。
所以只用求n!的质因数成绩中,5的个数即可。
n/5得到5个数,但像25,75.....这种有多个5的,就需要再n/5
class Solution { public: int trailingZeroes(int n) { int ans = 0; while (n) { ans += n/5; n /= 5; } return ans; } };
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
相关文章推荐
- Aircrack-ng之aireplay(Deauthentication)
- LeetCode之Contains Duplicate II
- 什么情况下用copy、assign、retain、strong...
- 车型识别“A Large-Scale Car Dataset for Fine-Grained Categorization and Verification”
- 刷一题Leetcode:Factorial Trailing Zeroes
- LeetCode之Contains Duplicate
- OC之非ARC环境下循环retain问题
- HDU 2476 String painter(区间dp)
- c的Main函数参数解析
- poj 1273 Drainage Ditches
- leecode 每日解题思路 127-Factorial Trailing Zeroes
- (null): Linker command failed with exit code 1 (use -v to see invocation)
- 为什么要内存对齐 Data alignment: Straighten up and fly right
- INSTALL_FAILED_CONFLICTING_PROVIDER错误解决方法
- 将 paired count 和unpaired count 相加
- rust用struct保存特定trait的引用
- 不同RAID模式的优缺点
- win7旗舰版卸载“获取Windows10”升级助手
- leetcode 172 Factorial Trailing Zeroes(难易度:Easy)
- 详解C语言中的wait()函数和waitpid()函数