https://leetcode.com/problems/factorial-trailing-zeroes/
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
解题思路:
再次遇见最讨厌的Math题。
开始的思路,结尾的0到底是哪来的?要有0,必须要乘积为10,那么可能2*5或者1*10,那么10又是2*5,所以就是去算有多少对2和5?再去看百度百科上20以内的阶乘 http://baike.baidu.com/view/245476.htm ,似乎也验证了有多少个5就有多少个0。因为2肯定比5多。
于是写下来下面的代码,该不会这么简单吧。
public class Solution {public int trailingZeroes(int n) {return n / 5;} }
果然错了。想不出来,只能去求助网友。
后来看见了Wikipedia-Trailing Zeroes,
The number of trailing zeros in the decimal representation of n!, the factorial of a non-negative integer n, is simply the multiplicity of the primefactor 5 in n!. This can be determined with this special case of de Polignac's formula:[1]
where k must be chosen such that
and denotes the floor function applied to a. For n = 0, 1, 2, ... this is
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 6, ... (sequence A027868 in OEIS).
For example, 53 > 32, and therefore 32! = 263130836933693530167218012160000000 ends in
zeros. If n < 5, the inequality is satisfied by k = 0; in that case the sum is empty, giving the answer 0.
也就是说,n!的结尾0的数量就等于n/5+n/25+n/125...
不过为什么除以5以后还要再除以25,除以125?显然因为25里有2个5,125里有3个5。但是为什么不是n/5+2*n/25+3*n/125...?因为n/5里面已经包含了25里的一个5了,同样n/25也包含了n/125里的一个5了。
于是写了以下代码
public class Solution {public int trailingZeroes(int n) {int base = 5, result = 0;;while(base <= n){result += n / base;base *= 5;}return result;} }
n=2147483647的时候,居然超时。原来是base*5到仅仅小于Integer.MAX_VALUE的时候,就超时了。
偷懒的将base申明为long,算是解决了。
public class Solution {public int trailingZeroes(int n) {long base = 5;int result = 0;while(base <= n){result += n / base;base *= 5;}return result;} }
其实n/base,base *= 5,不就是n/base,n /= 5吗?这样做更好点。
public class Solution {public int trailingZeroes(int n) {long base = 5;int result = 0;while(base <= n){result += n / base;n /= 5;}return result;} }
数学题真是弱啊,要重视。