TypechoJoeTheme

IT技术分享

统计

[LeetCode 172] Factorial Trailing Zeroes [Java] [Runtime : 1MS]

2017-09-02
/
0 评论
/
805 阅读
/
正在检测是否收录...
09/02

1. Description

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

Note: Your solution should be in logarithmic time complexity.

2. Runtime Distribution

3. Submission Details

4. Example

Input 25
Output 6

5. Code

public int trailingZeroes(int n) {
    if (n < 0) {
        return -1;
    }
    int count = 0;
    while (n != 0) {
        n /= 5;
        count += n;
    }
    return count;
}

6.Test

public class LeetCode0172 {
    public int trailingZeroes(int n) {
        if (n < 0) {
            return -1;
        }
        int count = 0;
        while (n != 0) {
            n /= 5;
            count += n;
        }
        return count;
    }

    public static void main(String[] args) {
        LeetCode0172 leetcode = new LeetCode0172();
        System.out.println(leetcode.trailingZeroes(25));
    }
}
Math
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/792/(转载时请注明本文出处及文章链接)