原题描述:

原题地址: Factorial Trailing Zeroes

题目描述很直接, 给出一个整数N, 求这个N的阶乘后尾有几个零。(要求O(logN)时间复杂度)

个人思路:

  一开始,最简单的思维就是直接求要知道, n!的增长速度, 比O(n^2)还要大, 对于32位整型来说, 当N=13的时候, 数据就已经开始溢出了,

  

  好吧, 就算使用long型也是到N=21时,表示数位也不够用了,

      

  那么, 这条路其实是走不通的, (就算考虑使用大数阶乘解决方案, 但这背离了这道题目的初衷,而且也达不到O(logN)的时间复杂度要求):

到这里, 我们先想想,1~10这十个数字,那些数相乘后有末尾零,也就是10的倍数?,显而易见的,只有碰到任意的偶数与5的倍数相乘是,才有得

才会多出一个零。 从而, 我们这边5的倍数这个元素就是关键点。

  其实,到了上一步,这个问题已经解决掉一半了, 剩下的工作就是求取给出的1~N个数里, 存在几个5的倍数, done!

  当时我就觉得问题已经解决,而且时间复杂度只有O(1)呢 : )

  

  马上提交, 结果呵呵:Wrong Anwser

  

  但是觉得30里不就6个5的倍数么, 得到的数尾应该就是6个零才对啊,然后我仔细盯着着这6个数:

  

  机智的朋友们应该一经发现了, 可是我却呆了一会才发现, 老子当时就是一拍大腿: "卧槽, 还有一种情况没有考虑!"

  

  没错, 就是这个罪魁祸首, 虽然他也是5的倍数, 但是他是5的n次数(包括其倍数, 例如25*4 = 100,100/10 = 10, 还是5的倍数,就是这种情况没考虑), 也就是意味他需要n次消化掉才不会有,  既然还要考虑到5的n次, 那么, 每次每隔5一次数, 然后再在结果中隔5取一次数, done!

  

  这次也是果断提交(时间复杂度 O(log(N)), 底数为5, 肯定比默认底数为2来的更快。):duang!

  另外, 关于执行速度, 貌似用C的话, 递归反而是最快的, 我估计是测试用例的问题吧, 反正不在今天的讨论范围,有兴趣的同学自己研究下,或者在评论区指教下,谢谢!

leecode 每日解题思路 127-Factorial Trailing Zeroes的更多相关文章

  1. leecode 每日解题思路 152 Maximun Product Subarray

    问题描述: 问题链接:152 Maximum Product Subarray 在经典的算法解析中, 有关的分治和动态规划的,经典题型之一就是求最大子段和, 这道题就是他的变形:求最大子段积; 这个问 ...

  2. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

  3. leecode 每日解题思路 102-Binary Tree Level Order Traversal

    題目描述: 题目链接: 102-Binary Tree Level Order Traversal 这个问题要解决的是如何逐层遍历一个二叉树,并把同一层元素放入同一list中, 再将所有元素返回. 其 ...

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

  5. 【LeetCode】172. Factorial Trailing Zeroes

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

  6. LeetCode Day4——Factorial Trailing Zeroes

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

  7. LeetCode Factorial Trailing Zeroes Python

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

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

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

  9. LeetCode_172. Factorial Trailing Zeroes

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

随机推荐

  1. c#基础这些你都看过吗?(一)-----仅供初学者使用

    1.注释(不写注释是流氓,名字瞎起是扯淡)‘///’一般用于注释函数,注释类.2.快捷键ctrl+k+d(有语法错误无法进行对齐)ctrl+j(快速弹出只能提示)shift+end,shift+hom ...

  2. BZOJ_1084_[SCOI2005]_最大子矩阵_(动态规划)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1084 给出一个n*m的矩阵,其中m<=2,取k个子矩阵,求最大子矩阵和. 分析 1.m= ...

  3. 使用 ASR 和 Azure Pack 为 IaaS 工作负荷提供托受管 DR

    Ashish Gangwar 云 + Enterprise项目经理 几周前,我们宣布了在 Azure Site Recovery 中提供一些新功能,这些新功能适用于不同场景,可以让服务提供商在 A ...

  4. 纯CSS实现侧边栏/分栏高度自动相等

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=694 一.为何要分栏高 ...

  5. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.1.1

    Given any $k$-tupel of linearly independent vectors $X$ as above, there exists a $k$-tuple $Y$ biort ...

  6. 社区商业试玩O2O:良渚文化村新街坊牵手阿里巴巴

    在电商时代,越来越多的人选择便捷的网上购物,使得实体商业受到了不小的冲击,各种大型的购物中心.购物广场已经不再那么人气十足,因此一些特色商业街区.社区商业频频出现,也不乏一些新玩儿法. 阿里巴巴(专题 ...

  7. POJ --- 2918 求解数独

    Tudoku   Description Tom is a master in several mathematical-theoretical disciplines. He recently fo ...

  8. Java笔记(二十)……线程间通信

    概述 当需要多线程配合完成一项任务时,往往需要用到线程间通信,以确保任务的稳步快速运行 相关语句 wait():挂起线程,释放锁,相当于自动放弃了执行权限 notify():唤醒wait等待队列里的第 ...

  9. HW5.15

    public class Solution { public static void main(String[] args) { System.out.printf("%10s\t%10s\ ...

  10. HW2.4

    import java.util.Scanner; public class Solution { public static void main(String[] args) { final dou ...