一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

(二)解题

题目大意:求n的阶乘算出来的数尾部有多少个0。如5!=120,尾部有1个0,返回1。

解题思路:仔细观察阶乘公式1*2*3….*n,只有2*5=10,这样才能有0,所以一开始想到的解法是算每个数的因子里面还有2和5的个数,这两个数组成一对就代表阶乘尾部有一个0,所以求这两个因子个数的最小值即可。

下面是TLE的版本,超时了。

class Solution {
public:
    int trailingZeroes(int n) {
        int count2 = 0;
        int count5 = 0;
        for(int i = 1 ;i <= n ;i++)
        {
            int temp = i;
            while(temp%2==0){//求因子2的个数
               count2++;
               temp/=2;
            }
            while(temp%5==0){//求因子5的个数
               count5++;
               temp/=5;
            }
        }
        return min(count2,count5);//返回较小值。
    }
};

超时之后,想了很久,在纸上推算了一下,发现根本不用取这两者的最小值,5的个数一定比2小,这样一来只需要判断因子5的个数就行。

如果想上述解法那样粗暴的判断每一个数中含有因子5的个数肯定是不行了。

于是想到5的因子基本上每隔5个数产生一个,n/5就能找出因子5的个数,

但是诸如25,125这种含有多个因子5的数,一次n/5肯定不对。还需要n/25才行……

这样一直推算下去,最有因子5的总个数为n/5+n/25+n/125+……

根据这个思路可以写下如下代码:

class Solution {
public:
    int trailingZeroes(int n) {
        int sum = 0;
        while(n){
           sum+=n/5;
           n/=5;
        }
        return sum;
    }
};

【一天一道LeetCode】#172. Factorial Trailing Zeroes的更多相关文章

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

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

  2. LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)

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

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

  4. ✡ 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 ...

  5. 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 log ...

  6. Java [Leetcode 172]Factorial Trailing Zeroes

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

  7. Leetcode 172 Factorial Trailing Zeroes

    给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...

  8. leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)

    数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...

  9. [LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数

    所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125.. ...

  10. LeetCode Day4——Factorial Trailing Zeroes

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

随机推荐

  1. 51Nod 1196 字符串的数量

    用N个不同的字符(编号1 - N),组成一个字符串,有如下要求: (1) 对于编号为i的字符,如果2 * i > n,则该字符可以作为结尾字符.如果不作为结尾字符而是中间的字符,则该字符后面可以 ...

  2. bzoj1531[POI2005]Bank notes 单调队列优化dp

    1531: [POI2005]Bank notes Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 559  Solved: 310[Submit][Sta ...

  3. JSP 基本语法

    1 JSP 的由来 servlet产生后,存在很大的问题,为了表现页面的效果,需要输出大量的HTML 语句,表现为一个个字符串,不仅利于开发,也不利于后期的维护,由此产生了JSP.主要用于将Servl ...

  4. python MySQLdb pymsql

    参考文档 https://www.python.org/dev/peps/pep-0249/#nextset 本节内容 MySQLdb  pymysql MySQLdb和pymysql分别为Pytho ...

  5. python正则表达式与Re库

    正则表达式是用来简洁表达一组字符串的表达式,一行胜千言,有点类似于数列的通项公式. 在python中提供了re库(regular expression)即正则表达式库,内置于python的标准库中,导 ...

  6. Javascript面向对象编程(一):封装

    Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言,因为它的语法中没有class(类). 那么,如果 ...

  7. mysql常见的优化需要注意的点

    1.explain分析explian引用索引基数show indexes from table_name;主键索引具有最好的基数 测试时 不走缓存SELECT SQL_NO_CACHE id from ...

  8. Linux下常用的配置

    本文主要给出的都是一些常用的Linux配置,系统版本是基于CentOs6.3,供自己复习和新人学习,不当之处还请指正. vmware tools安装 虚拟机--->安装vmware tools ...

  9. Java不走弯路教程(3.用户验证与文件内容查询)

    3.用户验证与文件内容查询 在上一章中,我们完成了对指定文件内容的输出操作. 我们现在有如下格式的文件product.db id,product_name,product_detail 1,noteb ...

  10. 重载equals方法时要遵守的通用约定--自反性,对称性,传递性,一致性,非空性

    本文涉及到的概念 1.为什么重载equals方法时,要遵守通用约定 2.重载equals方法时,要遵守哪些通用约定   为什么重载equals方法时,要遵守通用约定 Object类的非final方法都 ...