LeetCode 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.
题目标签:Math
题目要求我们找到末尾0的数量。
只有当有10的存在,才会有0,比如 2 * 5 = 10; 4 * 5 = 20; 5 * 6 = 30; 5 * 8 = 40 等等,可以发现0 和 5 的联系。
所以这一题也是在问 n 里有多少个5。
需要注意的一点是,25 = 5 * 5; 125 = 5 * 5 * 5 等等
Java Solution:
Runtime beats 42.46%
完成日期:01/16/2018
关键词:Math
关键点:0 和 5 的联系
class Solution
{
public int trailingZeroes(int n)
{
int zeros = 0; while(n > 0)
{
zeros += n / 5;
n = n / 5;
} return zeros;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)的更多相关文章
- ✡ 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 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)
数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...
- [LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数
所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125.. ...
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- 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 阶乘末尾0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 172 Factorial Trailing Zeroes 阶乘后的零
给定一个整数 n,返回 n! 结果尾数中零的数量.注意: 你的解决方案应为对数时间复杂度. 详见:https://leetcode.com/problems/factorial-trailing-ze ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- 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 ...
随机推荐
- XAMPP--Apache服务无法启动问题定位及处理
一.问题简述: XAMPP 在使用一段时间后,Apache服务无法启动. 二.详细描述: 安装Xampp服务器套件之后,部署使用正常.一段时间未使用,再次打开时,Apache服务无法启动.错误提示如下 ...
- Angular——$http
基本介绍 $http用于向服务端发起异步请求,同时还支持多种快捷方式如$http.get().$http.post().$http.jsonp.$hhtp也是属于内置服务的一种,这里特意提出来写一篇用 ...
- Vue指令6:v-show
根据表达式的真假值来渲染元素 用法大致一样: <h1 v-show="ok">Hello!</h1> 不同的是带有 v-show 的元素始终会被渲染并保留在 ...
- 简单工厂模式&工厂方法模式&抽象工厂模式的区别
之前写过一篇关于工厂模式(Factory Pattern)的随笔,里面分析了简单工厂模式,但对于工厂方法和抽象工厂的分析较为简略.这里重新分析分析三者的区别,工厂模式是java设计模式中比较简单的一个 ...
- 模板TemplateRef
TemplateRef<void> <ng-template #模板名称></ng-template>
- Microsoft SQL Server Transact-SQL
Microsoft SQL Server Transact-SQL 1.SQL 1.1数据定义语言(DDL) create 创建数据库或数据库对象:alter 修改数据库或数据库对象:drop 删除数 ...
- Appium 的xpath定位
Appium 的xpath定位 1.如果元素text是唯一的,可以通过text文本定位 //*[@text=’text文本属性’] # 定位text driver.find_element_by_xp ...
- (1) GoJS入门
GoJS的官方下载,若下载失败,可尝试通过我的个人网盘分享下载. GoJS是一款功能强大,快速且轻量级的流程图控件,可帮助你在JavaScript 和HTML5 Canvas程序中创建流程图,且极大地 ...
- HDU - 2044 - 一只小蜜蜂...(dp)
题意: 如题 思路: 仔细观察图 1-4和3-6其实是一样的答案,那么所有的方案都可以相减,意思为全部转化为从1开始 剩下的就是观察规律,仔细观察5号,能到5号蜂房的只有3和4,3和4到5号蜂房只有一 ...
- UVA-1599 Ideal Path(双向BFS)
题目: 给一个n个点m条边(2≤m≤100000, 1≤m≤200000)的无向图,每条边上都涂有一种颜色(用1到1000000000表示).求从结点1到结点n的一条路径, 使得经过的边数尽量少,在此 ...