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 ...
随机推荐
- python 用到的函数记录
1. ctime() 获取当前的时间 2. import random random.randint(0,99) 随机产生0到99之间的数值 (包含0和99) (整数!!) 3. 往列表添加数值 l ...
- 学习笔记之Kubernetes
Kubernetes | Production-Grade Container Orchestration https://kubernetes.io/ Kubernetes is an open-s ...
- [转]连连看游戏 C#
源代码下载地址 http://files.cnblogs.com/files/z5337/%E8%BF%9E%E8%BF%9E%E7%9C%8B%E6%B8%B8%E6%88%8F.rar 代码由 & ...
- make -j [N] --jobs [=N] 增加效率
阿里云的服务器,以前是最低配1核心cpu,make的时候非常慢.升级配置以后,发现make的效率丝毫没有增加.top命令查看发现cpu的利用率非常低,于是执行命令: make --help 在显示的结 ...
- [UE4]C++的const类成员函数
我们知道,在C++中,若一个变量声明为const类型,则试图修改该变量的值的操作都被视编译错误.例如: const char blank = ‘’; blank = ‘\n’; // 错误 要声明一个 ...
- Mysql 於lampp xampp LinuxUbuntu下的配置
默认执行Lampp/Xampp 於Ubuntu下完成后,需要对mysql进行一系列的配置,方可进行更好的操作 lampp下的mysql配置文件路径: /opt/lampp/etc/my.cnf 1 配 ...
- API网关Kong系列(一)初识
最近工作需要,加上国内Kong的文章相对缺乏(搜来搜去就那么两篇文章),而且官方文档在某些demo上也有一些过时的地方,遂提笔记录下这些,希望能有帮助. 先随大流介绍下KONG(主要参考官网): 官方 ...
- 第2课 C 到 C++ 的升级
1. C与C++的关系 (1)C++继承了所有的C特性,并在C的基础上提供了更多的语法和特性. (2)C++的设计目标是运行效率与开发效率的统一,它更强调的是语言的实用性. 2. C到C++ 的升级 ...
- sqoop操作之Oracle导入到HDFS
导入表的所有字段 sqoop import --connect jdbc:oracle:thin:@192.168.1.100:1521:ORCL \ --username SCOTT --passw ...
- python之路之装饰器
一 装饰器进化之路1) import time def index(): start_time=time.time() time.sleep() print('welcome to index wor ...