ACM题库~

LeetCode 172. Factorial Trailing

2017-10-23  本文已影响6人  关玮琳linSir

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

Note: Your solution should be in logarithmic time complexity.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

题意:找到阶乘结果最后面有多少个0

思路:判断这里面有多少个5就可以,也就是说在阶乘过程中可能出现的5

java代码:

class Solution {
    public int trailingZeroes(int n) {
        return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读