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 ...
随机推荐
- Unity 两张背景的切换平移
两张背景图片向左移动,当屏幕看见的时候. 使用的是Unity自带的Sprite,当然也可以使用NGUI Sprite using UnityEngine; using System.Collectio ...
- lua select
可以用这样的方法产生类似foreach的功能: function printargs(...) local num_args = select("#", ...) for i = ...
- ios像素点颜色取样
一.像素点颜色取样 + (UIColor*) getPixelColorAtLocation:(CGPoint)point inImage:(UIImage *)image { UIColor* co ...
- Linux以及Android开发中的小技巧和长繁命令记录收集
不断更新收集中.... 201407161654 ssh以nx_guest的身份登录到172.24.221.137,然后在172.24.221.137与172.24.61.252的8080port建立 ...
- js执行环境深入研究
js 声明函数是创建函数对象的过程,当创建函数对象时,函数对象的[[scope]] =连当前执行环境对象的作用域(栈顶执行环境--当执行函数时,js会将该函数的执行环境对象入栈) 当为全局函数时,如: ...
- nodejs实现接收Snmp的Trap消息
var assert = require('assert'); var ASN1 = { EOC: 0, Boolean: 1, Integer: 2, BitString: 3, OctetStri ...
- Sqlserver系列(二) 模糊查询 like
通配符 % 匹配零个或多个字符 _ 匹配单个字符 [] 指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符. [^] 不属于指定范围 ([a-f]) 或集合 ([abcdef] ...
- C#创建Windows服务与安装-图解
1.创建windows服务项目
- 区段extent及数据块
一.区段是表空间中由某个段所使用的一块磁盘空间.他是一组连续的oracle数据块.引入extent的目的是为了减少磁盘空间分配的次数,如果是采用oracle数据块直接分配的话就增加了oracle磁盘空 ...
- microwindows Win32 API demo
初次使用microwindows,资料有限,我也是费了很多功夫才明白.所以记录下来,好帮助那些爱学习的童鞋,另外请大虾们多多指教. 什么是microwindows,什么作用,等背景介绍我就不多说了,因 ...