Factorial Trailing Zeroes

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

Note: Your solution should be in logarithmic time complexity.

思路:實現很簡單,關鍵的分析題意,要求n!末尾的0數,事實上可以求5和2的對數。而又因爲出現5必定出現2,所以求5個數即可.

 int trailingZeroes(int n) {
        int count  = 0;
        n = n/5;
        while(n>0)
        {
            count+=n;
            n /=5;
        }
        return count;
    }


 

發佈了122 篇原創文章 · 獲贊 5 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章