美文网首页ACM题库~
LeetCode 172. Factorial Trailing

LeetCode 172. Factorial Trailing

作者: 关玮琳linSir | 来源:发表于2017-10-23 14:39 被阅读6次

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

相关文章

网友评论

    本文标题:LeetCode 172. Factorial Trailing

    本文链接:https://www.haomeiwen.com/subject/ahkauxtx.html