LeetCode:Factorial Trailing Zeroes
2016-06-15 12:17
513 查看
Factorial Trailing Zeroes
Total Accepted: 61757 TotalSubmissions: 185808 Difficulty: Easy
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
Hide Tags
Math
Hide Similar Problems
(H) Number of Digit One
思路:
1.题意求n!中后缀0的个数。
2.n!=1*2*3*...*n,中的0由(2^i) * (5^j)得来。
3.即要计算min(i,j)。
4.j<=i,直观的来看:i是逢2进1,j是逢5进1。固只需计算5的个数。
例如:10!=1*2*3*4*5*6*7*8*9*10
= ..*(2^4)*...*(5^2)..
求10!中5的个数,即求 k = n/5 + n/25 + n/125 + ....+n/5^j,其中j<=n。
c++ code:
class Solution { public: int trailingZeroes(int n) { int x = 5; int ret = 0; while(x <= n) { ret += n/x; x *= 5; } return ret; } };
或:
class Solution { public: int trailingZeroes(int n) { int ret = 0; while(n) { ret += n/5; n /= 5; } return ret; } };
相关文章推荐
- [Training Video - 1] [Selenium Basics] [Xpath Selenium]
- Cookie跨域setDomain和setPath
- Annotation之标记注解
- CDISC SDTM AE domain学习笔记 - 2
- Kodi ——7 Filling containers
- mongoDB Cannot start server. The default storage engine 'wiredTiger' is not available
- 理解 OpenStack + Ceph (9): Ceph 的size/min_size/choose/chooseleaf/scrubbing/repair 等概念
- Kodi ——6 Controls (26)6.26 Panel Container
- Kodi ——6 Controls (25)6.25 FixedList Container
- xcode换新版本,原来工程编译报错:linker command failed with exit code 1(use -v to see invocation)
- Kodi ——6 Controls (24)6.24 Wraplist Container
- Kodi ——6 Controls (23)6.23 List Container
- 在AIX8.0下实现ftp上传指定文件到ftp服务器
- List的contains()方法
- 李开复万字长文科普人工智能:AI是什么 将带我们去哪儿?
- 北美18名校的数据挖掘,数据分析,人工智能及机器学习课程汇总
- 北美18名校的数据挖掘,数据分析,人工智能及机器学习课程汇总
- 人工智能、机器学习和数据挖掘三者之间的关系
- 北美18名校的数据挖掘,数据分析,人工智能及机器学习课程汇总
- 北美18名校的数据挖掘,数据分析,人工智能及机器学习课程汇总