Leetcode 172. Factorial Trailing Zeroes
給定一個整數n,求n的階乘有多少個字尾0.
思路:n!=1*2*3*……*n,判斷這個數有多少個字尾0,通過觀察我們可以發現每當出現一個約數2和一個約數5,就可以產生一個字尾0,只要是偶數中都存在2這個約數,所以2的數目肯定比5多,所以只需要判斷約數5的個數,就是所求。需要注意的是,25、50這樣的約數裡面含有2個約數5.
方法1:迴圈
class Solution {
public:
int trailingZeroes(int n) {
int re=0;
while(n>=5)
{
n/=5;
re+=n;
}
return re;
}
};
方法2:遞迴
class Solution {
public:
int trailingZeroes(int n) {
return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
}
};
相關文章
- leetcode刷題--Factorial Trailing ZeroesLeetCodeAI
- Factorial Trailing Zeroes 階乘後的零AI
- 每天一道LeetCode--172. Factorial Trailing ZeroesLeetCodeAI
- [CareerCup] 17.3 Factorial Trailing Zeros 求階乘末尾零的個數AIROS
- LeetCode—283—Move ZeroesLeetCode
- Leetcode Set Matrix ZeroesLeetCode
- LeetCode 73 Set Matrix ZeroesLeetCode
- Leetcode-Set Matrix ZeroesLeetCode
- Set Matrix Zeroes leetcode javaLeetCodeJava
- Leetcode 73. Set Matrix ZeroesLeetCode
- 283. Move Zeroes--LeetCode RecordLeetCode
- leetcode學習筆記73 Set Matrix ZeroesLeetCode筆記
- 演算法Set Matrix Zeroes演算法
- [Algorithm] 2. Trailing ZerosGoAIROS
- error unexpected trailing commaErrorAI
- [CareerCup] 1.7 Set Matrix Zeroes 矩陣賦零矩陣
- Lintcode539 Move Zeroes solution 題解
- rust-quiz:029-tuple-trailing-commas.rsRustUIAI
- oracle sqlloader_控制檔案_trailing nullcols小記OracleSQLAINull
- sqlloader匯入log日誌的trailing nullcols的理解SQLAINull
- 【LeetCode】如何學習LeetCode?LeetCode
- LeetcodeLeetCode
- LeetCode in actionLeetCode
- Leetcode AnagramsLeetCode
- Leetcode SortListLeetCode
- Leetcode ReorderListLeetCode
- leetcode SubsetsLeetCode
- Leetcode PermutationsLeetCode
- [LeetCode刷題筆記] 關於LeetCode的前言LeetCode筆記
- 我的 LeetCodeLeetCode
- Leetcode學習LeetCode
- LeetCode 53 JSLeetCodeJS
- LeetCode 50 JSLeetCodeJS
- LeetCode 克隆圖LeetCode
- Leetcode Perfect SquaresLeetCode
- Leetcode Sort ColorsLeetCode
- Leetcode Sort ArrayLeetCode
- 【LeetCode】Jewels and StonesLeetCode