Java for LeetCode 172 Factorial Trailing Zeroes
2015-06-06 00:53
441 查看
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实现如下:
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; }
相关文章推荐
- SpringMVC 之@ModelAttribute
- MyEclipse 配置SSH框架
- Java I/O : Bit Operation 位运算
- Java interface 做函数参数
- Java for LeetCode 171 Excel Sheet Column Number
- Java for LeetCode 169 Majority Element
- 在控制台打印半径为R的圆
- 在控制台用星号打印一个高M的等腰三角形与菱形
- 魔术矩阵
- Java多线程、事件
- Spring @MVC configuration without XML
- MD5加密结果在不同的环境下出现不一致
- 【SSH进阶之路】Struts + Spring + Hibernate 进阶开端(一)
- Eclipse配色方案
- SSO单点登陆环境搭建
- java高级工程师需要掌握的技术
- 斐波那契堆(Fibonacci heap)原理详解(附java代码实现)
- 【JAVA基础知识总结】Java自定义异常
- 【JAVA基础教程】java中异常机制总结
- java project 连接hibernate 出错