您的位置:首页 > 编程语言 > Java开发

Leetcode:172. Factorial Trailing Zeroes(JAVA)

2016-03-08 11:14 459 查看
【问题描述】

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.
【思路】
关于N的阶乘后面有几个0,即Factorial Trailing Zeroes。

分解因子,当且出现一对2和5时,末尾添加一个0。且因子2多于因子5,固计算N的因子中包含多少5.

public class Solution {
public int trailingZeroes(int n) {
if (n <= 0) {
return 0;
}

int count = 0;
while (n / 5 != 0) {

n /= 5;
count += n;
}

return count;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode java