Leetcode 172 Factorial Trailing Zeroes
2017-01-11 15:03
393 查看
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
求阶乘的后缀0个数
乘法中的零来源于10,10来源于2和5,在阶乘中,一个数的质因子出现一次5,那么必然有其他数的质因子出现若干次2
所以问题变为求解质因子5出现的次数,
n/5求出包含一个5的数字个数
n/25求出包含两个5的数字个数...以此类推
class Solution {
public:
int trailingZeroes(int n) {
int res = 0;
while(n)
{
res += n/5;
n/=5;
}
return res;
}
};
Note: Your solution should be in logarithmic time complexity.
求阶乘的后缀0个数
乘法中的零来源于10,10来源于2和5,在阶乘中,一个数的质因子出现一次5,那么必然有其他数的质因子出现若干次2
所以问题变为求解质因子5出现的次数,
n/5求出包含一个5的数字个数
n/25求出包含两个5的数字个数...以此类推
class Solution {
public:
int trailingZeroes(int n) {
int res = 0;
while(n)
{
res += n/5;
n/=5;
}
return res;
}
};
相关文章推荐
- 一年后再次回头看container_of宏的作用
- 一年后再次回头看container_of宏的作用
- ver 0.83--train_cnn.m
- 当你在试衣间试衣服,请你务必想起wait()与notify()
- 深度学习前言综述
- Rails在MacOS上搭建Heroku部署环境
- Rails在MacOS上搭建Heroku部署环境
- Rails在MacOS上搭建Heroku部署环境
- com.google.android.gts.placement.CoreGmsAppsTest#testCoreGmsAppsPreloaded fail
- 管理aix的密码策略
- MSVCRTD.lib(crtexe.obj) : error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStart
- grails 查询
- Flyway Validate failed:migration checksum mismatch for migration 1.0.0003
- wait_event_interruptible与wake_up配套使用
- #1055 - Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column this i
- 如何解决SVN服务器端安装过程出现 “Custom action InstallWMISchemaExecute failed:无法启动服务,原因可能是已被禁用或与其相关联的设备没有启动。”
- Could not obtain transaction-synchronized Session for current thread
- ECharts 3.0底层zrender 3.x源码分析2-Painter(V层)
- LeetCode 256. Paint House
- linker command failed with exit code 1 (use -v to see invocation)