Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

172. Factorial Trailing Zeroes

Posted on 2020-05-19 | Edited on 2021-01-22

https://leetcode.com/problems/factorial-trailing-zeroes/

Explanation: ref.1 ref.2

1
2
3
4
5
class Solution {
public int trailingZeroes(int n) {
return n==0?0:n/5+trailingZeroes(n/5);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* factorial -> overflow
class Solution {
public int trailingZeroes(int n) {
String facNum=String.valueOf(fac((long)n));
int cnt=0;
for(int i=facNum.length()-1;i>=0;i--){
if(facNum.charAt(i)=='0') cnt++;
if(facNum.charAt(i)!='0') break;
}
return cnt;
}
public long fac(long n){
if(n<=1) return 1;
return n*fac(n-1);
}
}*/
<1…198199200…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%