您的位置:首页 > 大数据 > 人工智能

Leetcode 172. Factorial Trailing Zeroes

2016-05-30 22:05 411 查看


172. Factorial Trailing Zeroes

Total Accepted: 60237 Total
Submissions: 182176 Difficulty: Easy

Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.

思路:

0由2,5相乘得出。5的个数一定小于2。所以只用判断5的个数。需要注意的是5,10,15,20都只提供一个5,但是25提供了两个5!

public class Solution {
public int trailingZeroes(int n) {
int count = 0;
while(n>=5) {
n /= 5;
count += n;
}
return count;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode