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 ...
随机推荐
- 分布式一致性协议之:Raft算法
一致性算法Raft详解 背景 熟悉或了解分布性系统的开发者都知道一致性算法的重要性,Paxos一致性算法从90年提出到现在已经有二十几年了,而Paxos流程太过于繁杂实现起来也比较复杂,可能也是以为过 ...
- 导入testng管理测试用例
1.在pom.xml中增加testng的依赖,以导入testNG 2.在src-main-resources目录下新建xml文件,比如untitled.xml. <?xml version=&q ...
- xsl如何实现递归复制?
<xsl:template match="*" mode="addSeatSelectionToAirProduct"> <xsl:eleme ...
- no_unnest,push_subq,push_pred的用法 (转)
常常有人把这三个hint搞混,主要是因为对三种重写原理不清楚.特总结如下.(实验环境为10204)1. no_unnest, unnestunnest我们称为对子查询展开,顾名思义,就是别让子查询孤单 ...
- 那些你希望N年前就掌握的命令
这篇文章转载自黑客志,短短的一篇文章我找到了3个对我非常有用的技巧,在信息爆炸的今天,简直就跟捡宝似的,希望这些命令对你也有帮助. 有人在Reddit上发帖询问:有没有哪条命令是你希望自己在几年前就掌 ...
- linux操作系统2 linux常用命令
知识内容: 1.目录及文件操作 2.用户.群组与权限 3.重定向.管道 4.磁盘存储管理 5.系统命令 6.其他命令 参考: http://man.linuxde.net/ Linux命令规则:目录名 ...
- 时间模块 --- time
表示时间有三种方法:timestamps Format String struct-time 1 时间戳 :通常来说,时间戳表示的是从1970年一月一日00:00:00开始按秒计算的偏移量,使 ...
- UVA-673-栈-水题
题意: 检测括号是否匹配,注意有空格 #include<stdio.h> #include<iostream> #include <strstream> #incl ...
- Java的Synchronized
原理,注意看输入输出,不了解原理是想不到会这样输出的. http://www.cnblogs.com/paddix/p/5367116.html 还有一个要注意的是一个对象一个monitor类
- gvim下用Vundle安装solarized主题的方法
1.在.vimrc中加入 Bundle 'Solarized' 2.重启gvim,并执行 :BundleInstall 3.将solarized.vim文件放入.vim下的colors文件夹内(如果没 ...