分析
难度 易
来源
https://leetcode.com/problems/factorial-trailing-zeroes/
题目
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.
解答
1 package LeetCode; 2 3 public class L172_FactorialTrailingZeroes { 4 public int trailingZeroes(int n) { 5 long factor=5; 6 int res=0; 7 while(n/factor>0){//n不变,除数不断增大 8 //n=n/factor; 9 res+=n/factor; 10 factor*=5;//5的幂,如25,含有多个5 11 } 12 return res; 13 } 14 public static void main(String[] args){ 15 L172_FactorialTrailingZeroes l172=new L172_FactorialTrailingZeroes(); 16 System.out.println(l172.trailingZeroes(30)); 17 } 18 }