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

leetcode - 172. Factorial Trailing Zeroes

2018-02-01 13:22 267 查看
Problem:

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

解释:求n阶乘最后有几个0。时间复杂度要求logn。

Solve: 

因为10=2*5,如75=5*5*3,有两个5。且阶乘中2的数量肯定比5多。所以求解5的数量就行了。

时间复杂度O(logn),AC-1mspublic int trailingZeroes(int n) {
return n==0?0:trailingZeroes(n/5);
}
// public int trailingZeroes(int n) {//直观写法
// int result = 0;
//
// while(n!=0){
// n=n/5;
// result+=n;
// }
//
// return result;
// }后记:想不到有这样的计算方法,看了讨论区才知道的。技不如人。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: