leetcode刷题,总结,记录 ,备忘 172
2015-12-14 20:24
375 查看
leetcode172 Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
求阶乘的尾数0的个数。首先想到的就是2x5,才会得0,又由于2的数量是肯定比5多的,所以只要计算5的数量即可,一开始我也比较天真,直接计算n / 5的结果,但是不行,因为还有25的情况,是5的2次方,需要多加个1,后面还有125,625等。所以使用循环每次除以5,将结果累加。
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
求阶乘的尾数0的个数。首先想到的就是2x5,才会得0,又由于2的数量是肯定比5多的,所以只要计算5的数量即可,一开始我也比较天真,直接计算n / 5的结果,但是不行,因为还有25的情况,是5的2次方,需要多加个1,后面还有125,625等。所以使用循环每次除以5,将结果累加。
class Solution { public: int trailingZeroes(int n) { int sum = 0; while (n) { sum += n / 5; n /= 5; } return sum; } };
相关文章推荐
- 算法
- UML用例图
- 余弦定理的应用:基于文字的文本相似度计算
- 为什么深层神经网络难以训练
- Arrays类详解
- Linux内核spin_lock、spin_lock_irq、spin_lock_irqsave
- UML用例图
- CocoaAsyncSocket 示例程序分析
- 常见错误8-Mac OS X下Coda2无法保存文件
- Redis系列一:初识Redis
- Android消息机制源码解析(三)——消息循环器Looper
- ubuntu安装wireshark1.12.8——支持openflow版本
- 2429: [HAOI2006]聪明的猴子
- POJ 3050 Hopscotch 【穷竭搜索 DFS】
- c++智能指针:std::auto_ptr
- vbScript 备忘
- mysqlbinlog解析binlog乱码问题解密
- 第九周项目三----稀疏矩阵的三元组表示的实现及应用1
- 机器学习技法第二次作业编程题1
- 网络爬虫:爬取动态网页(例如javascript)