Leetcode 172. Factorial Trailing Zeroes

来源:互联网 发布:云熙家具软件 编辑:程序博客网 时间:2024/05/19 11:45

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;    }}


0 0