172. Factorial Trailing Zeroes LeetCode
2016-02-11 22:45
465 查看
题意:求n阶乘后有多少个连续的0.
题解:能出现0必然是因为2 * 5 = 10,然而1到n中出现因子2的次数显然要大于等于因子5出现的概率,因此我们只需要统计出现了多少个因子5就可以。5的话就出现在5,10,15…里面。5-20里面只有1个5,25之后的都有两个5.因此我们每次用n除以5就可以得到包含1个因子5的数的个数,然后除以5,把只包含1个因子5的数字去掉。重复这个过程。就能求出因子5的所有个数。
题解:能出现0必然是因为2 * 5 = 10,然而1到n中出现因子2的次数显然要大于等于因子5出现的概率,因此我们只需要统计出现了多少个因子5就可以。5的话就出现在5,10,15…里面。5-20里面只有1个5,25之后的都有两个5.因此我们每次用n除以5就可以得到包含1个因子5的数的个数,然后除以5,把只包含1个因子5的数字去掉。重复这个过程。就能求出因子5的所有个数。
class Solution { public: int trailingZeroes(int n) { int count = 0; while(n) { count += n / 5; n /= 5; } return count; } };
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解