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 ...
随机推荐
- C# 基础概念之延迟加载
本文来自:http://kb.cnblogs.com/page/99182/ 作者: 杨延成 来源: 博客园 发布时间: 2011-05-01 15:06 阅读: 4662 次 推荐: 0 ...
- C 语言中实现数据与方法的封装
在 C 语言中可以用结构体代替类,用函数指针代替成员方法,实现数据成员与成员方法的封装,在客户端写出的程序与 C++ 类似,唯一的不同是 C 语言中调用函数指针成员时必须将本对象的地址传给函数,因为 ...
- Exploring TCP state machine by graphs
States TCP includes 11 states, they are: LISTEN SYN_SENT SYN_RECV ESTABLISHED FIN_WAIT1 CLOSE_WAIT F ...
- VLC-Android和VLC几个关键宏定义的分析
在用SourceInsight分析VLC-Android源码过程中,有几个宏定义在源代码中一直没有找到出处,比如 HAVE_DYNAMIC_PLUGINS和__PLUGIN__,以及MODULE_NA ...
- onvif规范 中文介绍
什么是ONVIF ? ONVIF规范描述了网络视频的模型.接口.数据类型以及数据交互的模式.并复用了一些现有的标准,如WS系列标准等. ONVIF规范的目标是实现一个网络视频框架协议,使不同厂商所生产 ...
- 详解AJAX核心 —— XMLHttpRequest 对象 (下)
继续上一篇的内容上一篇关于XMLHttpRequest 对象发送对服务器的请求只说到了用Get方式,没有说Post方式的.那是因为要说Post方式就需要先说另外一个东西,那就是DOM(Document ...
- ios9基础知识(UI)总结
UIWindow.UILabel.UIColor.UIScreen.UIViewController.UIView.UIControl.UIButton.IBOutlet.IBAction.UISte ...
- Eclipse MyEclipse 复制项目 复制现有项目 复制功能相似项目
如果现在已经存在一个Java Web项目 ProjectA,现在想做另外一个项目,里面绝大部分功能和结构都可以复用,如果想通过复制的方法来,那么可以这么做: 1.到资源管理器中,将ProjectA文件 ...
- U - 神、上帝以及老天爷(第二季水)
Description HDU 2006'10 ACM contest的颁奖晚会隆重开始了! 为了活跃气氛,组织者举行了一个别开生面.奖品丰厚的抽奖活动,这个活动的具体要求是这样的: ...
- MYSQL插入处理重复键值的几种方法
当unique列在一个UNIQUE键上插入包含重复值的记录时,默认insert的时候会报1062错误,MYSQL有三种不同的处理方法,下面我们分别介绍. 先建立2个测试表,在id列上创建unique约 ...