1, 题目
给定一个整数 n,返回 n! 结果尾数中零的数量。
示例 1:
输入: 3输出: 0解释: 3! = 6, 尾数中没有零。
示例 2:
输入: 5输出: 1解释: 5! = 120, 尾数中有 1 个零.
说明: 你算法的时间复杂度应为 O(log n) 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/factorial-trailing-zeroes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2, 算法
object Solution {def trailingZeroes(n: Int): Int = {var count = 0var x = nwhile (x != 0) {count += x / 5x /= 5}count}}
class Solution:def trailingZeroes(self, n: int) -> int:count = 0while n != 0:count += n // 5n //= 5return count
