Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]
生活随笔
收集整理的這篇文章主要介紹了
Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
翻譯:
給定一個整數n,返回n!末尾的0的個數。
提示:你的解決方案應該運行在O(log(n))的時間復雜度。
分析:
我們知道要形成一個末尾的0,需要組合出2和5這兩個質因子。然而在n!中,質因子2的個數永遠大于5的個數。所以,問題轉換為對n!計算5的質因子個數。
代碼:
public class Solution {public int trailingZeroes(int n) {int s=0;while(n>=5){n/=5;s+=n;}return s; } }總結
以上是生活随笔為你收集整理的Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leet Code OJ 189. Ro
- 下一篇: Leet Code OJ 223. Re