您的位置:首页 > 大数据 > 人工智能

LeetCode Factorial Trailing Zeroes

2015-08-14 12:24 429 查看
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/

求factorial后结尾有多少个0,就是求有多少个2和5的配对。

但是2比5多了很多,所以就是求5得个数。但是有的5是叠加起来的比如 25,125是5的幂数,所以就要降幂。

e.g. n = 100, n/5 =20, n/25= 4, n/125=0,所以加起来就有24个0.

AC Java:

public class Solution {
public int trailingZeroes(int n) {
//count number of 5 and 2, but 2 is much more than 5, so just count 5
int res = 0;
while(n>0){
res += n/5;
n /= 5;
}
return res;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: