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)的更多相关文章

  1. 【LeetCode】172. Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

  2. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

  3. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

  4. LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)

    172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...

  5. LeetCode_172. Factorial Trailing Zeroes

    172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...

  6. 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 ...

  7. [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  8. LeetCode Factorial Trailing Zeroes

    原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...

  9. [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  10. 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 ...

随机推荐

  1. 【Ethereum】以太坊ERC20 Token标准完整说明

    什么是ERC20 token 市面上出现了大量的用ETH做的代币,他们都遵守REC20协议,那么我们需要知道什么是REC20协议. 概述 token代表数字资产,具有价值,但是并不是都符合特定的规范. ...

  2. [UE4]集合:TSet容器

    一.TSet<T>是什么 UE4中,除了TArray动态数组外,还提供了各种各样的模板容器.这一节,我们就介绍集合容器——TSet<T>.类似于TArray<T>, ...

  3. mongodb 如何区分大小写

    mongodb是区分大小写的,在做mongodb数据库操作是经常使用toUpperCase()等方法将value转换为大写存到数据库中 e.g. 在做数据库模糊查询时语句如下 db.COLLECTIO ...

  4. python读取文件中的字典

    import ast def file_read():    with open('D:\\pytharm\\jichuyufa\\day2\\pro_cty_con.txt', 'r', encod ...

  5. uva-10305-水题-拓扑排序

    输入n,m,n代表点数,m代表边数(i,j),排序时i在j前面,没出现的点随意排 #include <iostream> #include<stdio.h> #include& ...

  6. python2-python3字符串

    https://www.cnblogs.com/yangmingxianshen/p/7990102.html

  7. 6. 纯 CSS 绘制一颗闪闪发光的璀璨钻石

    原文地址:https://segmentfault.com/a/1190000014652116 HTML代码: <div class="diamond"> <s ...

  8. leetcode324

    class Solution { public void wiggleSort(int[] nums) { int[] temp = Arrays.copyOfRange(nums, 0, nums. ...

  9. leetcode521

    public class Solution { public int FindLUSlength(string a, string b) { : Math.Max(a.Length, b.Length ...

  10. SQL 2012 分页取数据

    ,), data int ) select * from t1 row rows only create clustered index t1c on t1(id) declare @i int ) ...