Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]
2016-03-07 12:24
423 查看
题目:
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
翻译:
给定一个整数n,返回n!末尾的0的个数。
提示:你的解决方案应该运行在O(log(n))的时间复杂度。
分析:
我们知道要形成一个末尾的0,需要组合出2和5这两个质因子。然而在n!中,质因子2的个数永远大于5的个数。所以,问题转换为对n!计算5的质因子个数。
代码:
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
翻译:
给定一个整数n,返回n!末尾的0的个数。
提示:你的解决方案应该运行在O(log(n))的时间复杂度。
分析:
我们知道要形成一个末尾的0,需要组合出2和5这两个质因子。然而在n!中,质因子2的个数永远大于5的个数。所以,问题转换为对n!计算5的质因子个数。
代码:
[code]public class Solution { public int trailingZeroes(int n) { int s=0; while(n>=5){ n/=5; s+=n; } return s; } }
相关文章推荐
- 简单总结RectF、Rect 和Matrix ,还有Paint的使用方法
- sleep和wait区别以及线程池简单实现示例
- You have new mail in /var/spool/mail/root
- UVA 11374(p329)----Airport Express
- mybaits where 语句中使用<if></if>注意事项
- 软件测试(二)之 Failure, Error & Fault
- 【软件测试】作业二:fault & error & failure
- Error (-17998): Failed to get [param not passed in call] thread TLS entry
- 如何在 Linux 上使用 Gmail SMTP 服务器发送邮件通知
- 如何在 Linux 上使用 Gmail SMTP 服务器发送邮件通知
- HUST 1606 Naive
- csu 1685: Entertainment Box【湖南省多校对抗赛3.6】multiset妙用
- installation failed since the device possibly has stale dexed jars that don't match the current vers
- Develop -- Training(十一) -- 通过NFC共享文件
- How the Main Thread works
- 严重: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"]
- FZU 1001 Duplicate Pair(简单题目)
- 例题6-22 UVA 11853 Paintball 战场
- web service中chain的使用
- 软件测试(二)——分析程序中的错误(Fault,Failure,Error)