LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zeroes in n!. * Note: Your solution should be in logarithmic time complexity. */ /* * Solution 1 * 对于每一个数字,累计计算因子10、5、2数字出现的个数,结果等于10出现的个数,加上5和2中出现次数较少的 * 改进:5出现的次数一定大于2出现的次数,因此只需要统计因子10和5出现的次数。进一步衍生为只统计5出现的次数。 * 改进:讲循环控制的步长改为5 */ int trailingZeroes(int n) { ; ; ; int temp; ; i--) { temp = i; ) { == ) { number10++; temp = temp/; } == ) { number5++; temp = temp/; // } else if (temp % 2 == 0) { // number2++; // temp = temp/2; } else { break; } } } return (number5>number2?number2:number5)+number10; } /* * Solution 2 * 根据上面分析,只需要统计所有数字中因子5出现的次数 */ int trailingZeroes(int n) { ; ; ) { result += temp; temp = temp/; } return result; }
LeetCode Day4——Factorial Trailing Zeroes的更多相关文章
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 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 ...
- 【leetcode】Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- ✡ 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 ...
- 【leetcode】Factorial Trailing Zeroes(easy)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 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 ...
- leetcode:Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. 最初的代码 class Solution { public: int t ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
随机推荐
- linux 修改文件、文件夹权限
# change owner of all the fies under dirName chown -R username dirName #change owner and the file gr ...
- HDU 4978 A simple probability problem
A simple probability problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- H5单页面架构:requirejs + angular + angular-route
说到项目架构,往往要考虑很多方面: 方便.例如使用jquery,必然比没有使用jquery方便很多,所以大部分网站都接入类似的库: 性能优化.包括加载速度.渲染效率: 代码管理.大型项目需要考虑代码的 ...
- Linux 下的 Nginx 反向代理配置.
最近实践中遇到了需要利用 nginx 进行反向代理服务器请求的需求,以前没怎么碰触过,因此花了1个多小时,快速阅览了一下nginx官网在反向代理服务中给出的基本定义: 说实话,官网给予的定义是精准的, ...
- js获取昨天日期
刚刚js做项目,遇到需要获取昨天日期的问题,网上找了下答案,感觉网上的答案都不太严谨,自己写了个,凑合能用吧,忘大神们抛砖指教. <script type="text/javascri ...
- PlSql复制角色、权限和添加角色权限
一.登录你想要复制数据库的用户
- exist和not exist用法
参考:http://wenku.baidu.com/view/577f4d49cf84b9d528ea7a6f.html //这个讲的很详细 引用自:http://chenling1018.bl ...
- IE10以下placeholder不兼容
做页面的时候在谷歌中是显示的,但是换了IE之后总是不显示,一个文本框还好,如果有多个的话,如图: 添加以下一段Jquery代码: <script> var JPlaceHolder = { ...
- Android 创建自定义布局
我们所有的控件都是继承至View类的,而所有的布局都是继承至ViewGroup的,所以我们也可以继承某个view类来实现我们自己的布局或者控件. 引入布局 我们新建一个title.xml的layout ...
- Object转换为JSON格式字符串
简介: 把JS的Object转换为Json字符串. 代码: function ObjectToJson(object) { // Object转换为josn var json = "&quo ...