[leetcode][math] Factorial Trailing Zeroes
2015-06-02 12:06
441 查看
题目;
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.
class Solution { public: /*n!=1*2*3*4*5*6*7*8*9*10*11...*(n-1)*n,*/ /*分析:只有2*5能使得阶乘的末尾产生0,而2的个数一定比5多,所以问题转化成了在这个阶乘额计算式中有多少个5,我们发现,每5个数之中就多一个5(5, 10, 15,20,25...),而且每25(5*5)个数之中又多一个5(之前算25的时候只算了其中一个5), 每125(5*5*5)个数之中又多一个5...*/ int trailingZeroes(int n) { int res(0); while(n){ res += n/5; n /= 5; } return res; } };
相关文章推荐
- JMeter报错the target server failed to respond--JMeter的坑
- links_container_together
- [leetcode] Factorial Trailing Zeroes
- 在pentaho4.8中插件Saiku保存为中文文件名
- Contains DuplicateII
- Contains Duplicate
- getaddrinfo(),freeaddrinfo(), gai_strerror()函数详解
- 03-树2. Tree Traversals Again (25)
- Leetcode Contains Duplicate I, II, III
- explain 的详细说明
- RAID
- leetcode - Contains Duplicate III
- SAP内向交货详解 Details on SAP inbound delivery
- 领域驱动设计(Domain Driven Design)参考架构详解
- grails项目输出日志设定
- 强人工智能之“全本的鹦鹉”
- leetcode 220: Contains Duplicate III
- BeginWaitCursor()、EndWaitCursor()和RestoreWaitCursor()3个成员函数处理等待光标
- 台球游戏的核心算法和AI(2)
- LeetCode Contains Duplicate II