首页 > 编程语言 > 详细

Java for LeetCode 172 Factorial Trailing Zeroes

时间:2015-06-06 07:55:25      阅读:275      评论:0      收藏:0      [点我收藏+]

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

Note: Your solution should be in logarithmic time complexity.

解题思路:

计算n能达到的5的最大次幂,算出在这种情况下能提供的5的个数,然后减去之后递归即可,JAVA实现如下:

	static public int trailingZeroes(int n) {
		if(n<25)
			return n/5;
		long five=5;
		int count=0;
		while(n>=five){
			five*=5;
			count++;
		}
		int temp=(int) (n/Math.pow(5, count));
		return countSum(count)*temp+trailingZeroes(n-temp*(int)Math.pow(5, count));
	}
	static public int countSum(int count){
		if(count==1)
			return 1;
		else return countSum(count-1)*5+1;
	}

 

Java for LeetCode 172 Factorial Trailing Zeroes

原文:http://www.cnblogs.com/tonyluis/p/4555912.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!