2.Trailing Zeros-尾部的零(容易题)
来源:互联网 发布:犀牛软件 手机 编辑:程序博客网 时间:2024/05/16 07:14
尾部的零
题目
设计一个算法,计算出n阶乘中尾部零的个数
样例
11! = 39916800,因此应该返回 2
挑战
O(logN)的时间复杂度
题解
先把n除以5获取可以被5整除个数,然后再次除以5获取可以再次被5整除的个数,如此迭代处理直至没有可以被5整除的数。
如11/5 = 2,再将2/5=0,2+0=2即时答案。
class Solution { /* * param n: As desciption * return: An integer, denote the number of trailing zeros in n! */ public long trailingZeros(long n) { long sum = 0; while (n != 0) { sum += n / 5; n /= 5; } return sum; }};
Last Update 2016.8.12
0 0
- 2.Trailing Zeros-尾部的零(容易题)
- LintCode Trailing Zeros 尾部的零
- [LeetCode] 零的个数 number of trailing zeros
- 2.尾部的零
- 2.尾部的零
- LeetCode OJ 之 Factorial Trailing Zeroes (阶乘尾部的零)
- 172. Factorial Trailing Zeroes (计算n的阶乘尾部有多少个零)
- LintCode 2.尾部的零
- LintCode 2. 尾部的零
- Lintcode 2. 尾部的零
- lintcode2-2. 尾部的零
- 算法题:尾部的零
- 算法题:尾部的零
- Trailing Zeros
- (lintcode)第2题尾部的零
- 题目:尾部的零
- LintCode 尾部的零
- LintCode_尾部的零
- Struts2拦截器介绍及说明
- 大数据_Kafka_Kafka的Topic常用命令
- [Struts2官方指南的个人学习和翻译] Struts2自带例子MailReader的学习
- 位操作求余数
- CISSP笔记04
- 2.Trailing Zeros-尾部的零(容易题)
- android关于在根节点设置软键盘状态的属性
- listview中设置CheckBox引起的混乱问题
- 使用nexus搭建Maven私服
- 【HDU 3072】【通讯】【强连通分量】
- ImageLoader 加载图片
- 使用Git遇到的问题及解决方案(初级)
- Highcharts 实现堆叠图下钻两层的方法
- android 设置壁纸基本方法