Java for LeetCode 172 Factorial Trailing Zeroes
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的更多相关文章
- Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes
题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Leetcode 172 Factorial Trailing Zeroes
给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...
- leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)
数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...
- [LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数
所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125.. ...
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
随机推荐
- Git环境的搭建及使用
管理工具 1. Git环境的搭建 a.下载Git installer,地址:http://git-scm.com/downloads a1.参考文档地址:http://www.open-open.co ...
- 【POJ 2250】Compromise(最长公共子序列LCS)
题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出. #include<cstdio> #include<cstring&g ...
- JAVA TIMER定时器
备注:类实现ServletContextListener,在web.xml配置,之后服务启动该定时器类自动加载 package com.leadlt.common.util; import java. ...
- Aop 是面向切面编程,
Aop 是面向切面编程,是在业务代码中可以织入其他公共代码(性能监控等),现在用普通的方法实现AOP http://blog.csdn.net/heyanfeng22/article/details/ ...
- 网络html查看器
1)演示效果:
- MySQL存储过程解析
1.1 创建存储过程 MySQL中,创建存储过程的基本形式如下: CREATE PROCEDURE sp_name ([proc_parameter[,...]]) [charac ...
- 批量删除亚马逊kindle云端文档
首先鄙视亚马逊的不负责任,kindle的云端管理系统犹如一坨狗屎,根本没有考虑的任何用户体验,只能一个一个删除不说,删除后又回到第一页...翻页也没有输入页码的地方,如果在第100页删除文档后,又回到 ...
- 复合主键@IdClass
有时一个实体的主键可能同时为多个,例如同样是之前使用的“CustomerEO”实体,需要通过name和email来查找指定实体,当且仅当name和email的值完全相同时,才认为是相同的实体对象.要配 ...
- 新浪微博客户端(13)-使用UIWebView加载OAuth授权界面
使用UIWebView加载OAuth授权界面 DJOAuthViewController.m #import "DJOAuthViewController.h" @interfac ...
- 关于设置SQLPLUS提示符样式的方法----登陆配置文件,动态加载提示符
工作中用到 sqlplus mdsoss/mdsoss, 所以来了解一下sqlplus (C shell .cshrc文件里中alisa) 关于设置SQLPLUS提示符样式的方法 12638阅读 1评 ...