Factorial Trailing Zeroes
约 1 分钟· 本文阅读 次
本页目录
Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
Solution
计算 5 出现的次数即可
Code
public class Solution {
public int trailingZeroes_1(int n) {
int res = 0;
while (n != 0) {
res += n / 5;
n = n / 5;
}
return res;
}
public int trailingZeroes_2(int n) {
if (n == 0) return 0;
return n / 5 + trailingZeroes_2(n / 5);
}
}相关文章
- 100 Lockers2016-01-16
- Add Digits2016-01-16
- Ants on a Triangle2016-01-16
- The Apocalypse2016-01-16
已恢复到上次阅读位置