leecode 每日解题思路 127-Factorial Trailing Zeroes
原题描述:
原题地址: 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的更多相关文章
- leecode 每日解题思路 152 Maximun Product Subarray
问题描述: 问题链接:152 Maximum Product Subarray 在经典的算法解析中, 有关的分治和动态规划的,经典题型之一就是求最大子段和, 这道题就是他的变形:求最大子段积; 这个问 ...
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- leecode 每日解题思路 102-Binary Tree Level Order Traversal
題目描述: 题目链接: 102-Binary Tree Level Order Traversal 这个问题要解决的是如何逐层遍历一个二叉树,并把同一层元素放入同一list中, 再将所有元素返回. 其 ...
- 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】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!. ...
随机推荐
- ContentLoadingProgressBar不显示问题
ContentLoadingProgressBar需要设置style 并且在XML中布局的位置必须写在content布局的下面 <?xml version="1.0" enc ...
- Android最佳实践指南
Updated on 2016/1/6 修正了一些翻译段落欢迎转载,但请保留译者链接:http://www.jianshu.com/p/613d28a3c8a0 Lessons learned fro ...
- exynos4412电路OrCAD问题
图(1) K4B4G1646B,DDR3 图(2) JIA排针 问题:理论上,引脚命名不能重复,图(1)和图(2)都存在重复的引脚命名,但在进行DRC检查和网表输出时图(1)无任何问题,图(2)却有错 ...
- C# SerialPort的简单使用
SerialPort中串口数据的读取与写入有较大的不同.由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取.一.线程实时读串口:二.事件触发方式实现.由于线程实时读串口的效率不是十分高效 ...
- 借助 ASR 和 System Center 执行基于 SAN 复制的企业级灾难恢复
Abhishek A. Hemrajani 云 + Enterprise项目经理 对于Azure Site Recovery,我们的优先级已经提升为每个人都可以在任意地点获取易于使用的灾难恢复 (DR ...
- javascript 中concat与push的区别
1. push 遇到数组参数时,把整个数组参数作为一个元素:而 concat 则是拆开数组参数,一个元素一个元素地加进去. 2. push 直接改变当前数组:concat 不改变当前数组. 总结:如果 ...
- Get ListView items from other windows z
This is more difficult than one might think. In order to get the information you're looking for, you ...
- QT学习(对话框)codeblock版本
参考: http://www.cnblogs.com/JohnShao/archive/2011/09/26/2191627.html http://www.cnblogs.com/xiao-chen ...
- vxworks一个超级奇怪的错误(parse error before `char')
void tserver(int id) { if(debug){ useResource(,id);} char msgBuf[MAX_MSG_LEN]; if (msgQReceive(myMsg ...
- hotplug\uevent机制(1)
hotplug就是热拔插,在linux里面,这个功能是通过class_device_create这个函数来实现的,那么我们来分析下这个函数: class_device_create(cls, NULL ...