## 一.題目描述
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
## 二.題目分析
題目的要求是,給定一個整數`n`,找出`n!`結果的末尾為`0`的數的個數。暴力法是首先求出`n!`,然后直接計算末尾`0`的個數。(重復(n!)/10,直到余數非0為止),若輸入的`n`?值過大時,在計算階乘時會導致溢出,因此該方法并不好用。
[http://www.cnblogs.com/ganganloveu/p/4193373.html](http://www.cnblogs.com/ganganloveu/p/4193373.html)?給出一種很巧妙的解決方法:
事實上,只要對`n`的階乘`n!`,做因數分解:`n!=2^x*3^y*5^z*...`
顯然`0`的個數等于`min(x,z)`,并且經證明,可以得到:`min(x,z)==z`?。
例如:
`n = 5`時,`5!`的質因數分解:`5! = 2 * 2 * 2 * 3 * 5`?中包含`3`個`2`、`1`個`3`和`1`個`2`。后綴`0`的個數是`1`。
`n = 11`時,`11!`的質因數分解:`11! = 2^8 * 3^4 * 5^2 * 7`?中包含`8`個`2`、`4`個`3`和`2`個`5`。后綴`0`的個數是`2`。
證明:
對于階乘而言,也就是`1*2*3*...*n`,設`[n/k]`代表`1~n`中能被k整除的個數?
顯然有,`[n/2] > [n/5]`?(左邊是逢`2`增`1`,右邊是逢`5`增`1`)?
`[n/2^2] > [n/5^2]`(左邊是逢`4`增`1`,右邊是逢`25`增`1`)?
……?
`[n/2^p] > [n/5^p]`(左邊是逢`2^p`增`1`,右邊是逢`5^p`增`1`)?
隨著冪次`p`的上升,出現`2^p`的概率會遠大于出現`5^p`的概率。?
因此左邊的加和一定大于右邊的加和,也就是`n!`質因數分解中,`2`的次冪一定大于`5`的次冪。
## 三.示例代碼
~~~
#include <iostream>
using namespace std;
class Solution{
public:
int trailingZeroes(int n) {
int result = 0;
while (n)
{
result += n / 5;
n /= 5;
}
return result;
}
};
~~~
## 四.小結
這道題的代碼很少,但是要從題目中把復雜的要求轉化為簡單的運算是需要推導的,總體來說還是需要費一定的功夫。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016騰訊軟件開發筆試題)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契數列問題)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波蘭式的計算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和問題)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching