Factorial Trailing Zeroes (Divide-and-Conquer)
QUESTION
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
FIRST TRY
class Solution {
public:
int trailingZeroes(int n) {
int divident;
int nOf2 = ;
int nOf5 = ;
while(n% == )
{
nOf2++;
divident = n/;
}
while(n% == )
{
nOf5++;
divident = n/;
}
return min(nOf2,nOf5);
}
};
Result: Time Limit Exceeded
Last executed input: 0
SECOND TRY
考虑n=0的情况
class Solution {
public:
int trailingZeroes(int n) {
int divident;
int nOf2 = ;
int nOf5 = ;
for(int i = ; i < n; i++)
{
divident = i;
while(divident% == )
{
nOf2++;
divident /= ;
}
divident = i;
while(divident% == )
{
nOf5++;
divident /= ;
}
}
return min(nOf2,nOf5);
}
};
Result: Time Limit Exceeded
Last executed input:1808548329
THIRD TRY
2肯定比5多
要注意的就是25这种,5和5相乘的结果,所以,还要看n/5里面有多少个5,也就相当于看n里面有多少个25,还有125,625...
class Solution {
public:
int trailingZeroes(int n) {
if(n==) return ;
int divident=n;
int nOf5 = ; while(divident!= )
{
divident /= ;
nOf5+=divident;
} return nOf5;
}
};
Result: Accepted
Factorial Trailing Zeroes (Divide-and-Conquer)的更多相关文章
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...
- LeetCode_172. Factorial Trailing Zeroes
172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...
- LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- LeetCode Factorial Trailing Zeroes
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- 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 ...
随机推荐
- SpringMVC 获取请求参数
1.获取Request response对象 在SpringMVC的注解开发中,可以选择性的接收Request和Response对象来使用 2.获取request对象请求参数 a.通过request对 ...
- ASP.NET基于Redis的Provider库
因为session基于本地cache,以前我们自己写分布式缓存,或者数据库存储,或者cookie加密存储,来保存用户状态信息,但较少的直接通过创建一个继承 SessionStateStoreProvi ...
- hive的安装,一般不容易察觉的hdfs的配置问题导致hive安装的失败
在安装hive的过程中,一般需要的准备环境就是hadoop集群的正常启动,要装有mysql,zookeeper. 具体怎么安装配置我在这里不多说,安装教程网上有挺多的. 我这里说下我遇到的问题,首先从 ...
- linux 常规操作EOF写法梳理
在平时的运维工作中,我们经常会碰到这样一个场景:执行脚本的时候,需要往一个文件里自动输入N行内容.如果是少数的几行内容,还可以用echo追加方式,但如果是很多行,那么单纯用echo追加的方式就显得愚蠢 ...
- java技术-重点方向
多线程 锁 事务 缓存 hashmap 并发编程
- zabbix监控windows用户登陆情况
https://yq.aliyun.com/articles/511381 添加登录失败监控项: 特别注意:把类型设置为:文本格式,否则会报类型错误. eventlog[Security,," ...
- windows配置ftp服务器
一.搭建FTP 二.解决FTP因windows防火墙拦截的方法 三.配置FTP用户 ========================================================== ...
- leetcode66
public class Solution { public int[] PlusOne(int[] digits) { ]; < ) { digits[digits.Length - ]++; ...
- MS sql 无法进行事务日志备份
问题出在:恢复模型没有设置好我们之所以要备份数据库和事务日志都是为了以防万一,用来恢复.还原数据库的,因此恢复模型必须设置好,在sql server中默认的恢复模型为"简单".一般 ...
- Maven的几个常用plugin
出自:https://www.cnblogs.com/zhangxh20/p/6298062.html maven-compiler-plugin 编译Java源码,一般只需设置编译的jdk版本 &l ...