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 ...
随机推荐
- IOS ViewController 生命周期
加载过程 第一步 -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 第二步 -(void) ...
- Java程序员面试题集(1-50)(转)
转:http://blog.csdn.net/jackfrued/article/details/17339393 下面的内容是对网上原有的Java面试题集及答案进行了全面修订之后给出的负责任的题目和 ...
- Error Correct System(模拟)
Error Correct System Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- Keepalived+Lvs+Mysql主主复制
一简单介绍 Keepalived+lvs+mysql主主复制是比較经常使用的一种Mysql高可用方案,当中lvs 提供读负载均衡,Keepalived通过虚拟vip漂移实现故障自己主动转移,而Mysq ...
- Oracle Enterprise linux 7 安装Oracle11gR2
一.修改主机名和IP地址: [root@localhost Desktop]# cat /etc/hosts127.0.0.1 localhost.localdomain localhost 192. ...
- JAVA HashMap与HashTable 区别
HashTable和HashMap区别 第一,继承不同. public class Hashtable extends Dictionary implements Mappublic class Ha ...
- 流媒体开发之--HLS--M3U8解析(2): HLS草案
目录 1 简介 2 2 概述 2 3 播放列表文件 3 3.1 介绍 3 3.2新标签 4 3.2.1 EXT-X-TARGETDURATION 4 3.2.2 EXT-X-MEDIA-SEQUENC ...
- JavaScript脚本语言的正则校验法
正则校验法有很多种类型,有些可能会比较复杂难记,我这里罗列了大家常用的几种方法,方便查询. //校验是否全由数字组成 function isShuZi(s) { var patrn=/^[0-9]{1 ...
- typedef和define的作用域
typedef: 如果放在所有函数之外,它的作用域就是从它定义开始直到文件尾: 如果放在某个函数内,定义域就是从定义开始直到该函数结尾: #define: 不管是在某个函数内,还是在所有函数之外,作用 ...
- Debian 使用杂记(一)
前几天又冲动的把系统换成Linux了,最开始接触Linux是2010年,那时候买了个本本,预装的是ubuntu8.10,自此知道除了windows原来还有其它操作系统. 不记得什么时候开始知道ubun ...