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 ...
随机推荐
- Modulo Sum(背包 + STL)
Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- UVa 1394: And Then There Was One
设置一个数组Winner记录经典约瑟夫问题中的剩余者即可递归解决该问题. 注: 约瑟夫问题:有编号为0~n-1的n个人,从0号开始报数1,2,3......报到k的杀死,然后从下一个人开始继续报数1, ...
- Sublime Text 2 介紹
代码编辑器或者文本编辑器,对于程序猿来说,就像剑与战士一样,谁都想拥有一把能够随心驾驭且瑞丽无比的宝剑,而每一位程序猿,相同会去追求最适合自己的强大.灵活的编辑器,相信你和我一样,都不会例外. 我用过 ...
- lua 类继承和实现
http://blog.csdn.net/ssihc0/article/details/7742323 Account={balance=}; --新建了一个对像,他有一个属性balance func ...
- 搭建discuz论坛(2)
修改host文件: 使用ab做性能测试: E:\Apache2.2\bin>ab -n1000 -c100 http://bbs.ct.gd/
- php中数字和字母生成随机字符串
function strrand($len) { $arr = array( "0", "1", "2", "3", & ...
- Android 屏蔽ScrollView滑动操作
屏蔽ScrollView滑动操作,如下,会用到ViewConfiguration这个类,这个类可以获取到用户是否为滑动操作的临界值. 代码如下: package com.xx.uikit.view; ...
- Android 底部Dialog显示
public void showComplainDialog() { ComplainDialog complain_dialog = new ComplainDialog(OrderDetialAc ...
- CentOS6.4 LAMP环境搭建
网上的教程,不能按着抄打进去,这样会打乱你环境放置位置, 会导致配置路径会出问题. 要有一个环境目录优化, 把环境文件都装在/usr/local里面 首先,把安装文件rar都放置在/usr/local ...
- cocos2d-x中的Box2D物理引擎
在Cocos2d-x中集成了2个物理引擎,一个是Chipmunk,一个是Box2D.前者是用C语言编写的,文档和例子相对较少:Box2D是用C++写的,并且有比较完善的文档和资料.所以在需要使用物理引 ...