L:eetCode #172 - Factorial Trailing Zeroes

xiaoxiao2021-03-01  4

题目描述:

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

Example 1:

Input: 3 Output: 0 Explanation: 3! = 6, no trailing zero.

Example 2:

Input: 5 Output: 1 Explanation: 5! = 120, one trailing zero.

Note: Your solution should be in logarithmic time complexity.

想要尾数为0,必须要有2、5或者10相乘才行,而5的个数肯定小于2,所以只需要统计1~n的范围内,尾数为5的个数和10的个数。

class Solution { public: int trailingZeroes(int n) { int result=0; while(n>0) { result+=n/5; n/=5; } return result; } };

 

转载请注明原文地址: https://www.6miu.com/read-3850305.html

最新回复(0)