[LeetCode] 172. Factorial Trailing Zeroes_Easy tag: Math
Given an integer n, return the number of trailing zeroes in n!.
Example 1:
Input: 3
Output: 0
Explanation: 3! = 6, no trailing zero.
Example 2:
Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Note: Your solution should be in logarithmic time complexity.
思路是因为Because all trailing 0 is from factors 5 * 2, 然后2这个因子总是足够的, 比如2, 4, 6, 每两个就有个2的因子, 然后我们只需要数5的个数即可, 另外要反复循环直到没有5为止.
比如1,2,3,4,5,6,7,8,9,10
发现 1-4: 0
5-9: 1
10-14: 2
Code(O(lgn))
class Solution:
def FactorialZeroTail(self, n):
return 0 if n == 0 else n//5 + self.FactorialZeroTail(n//5)
[LeetCode] 172. Factorial Trailing Zeroes_Easy tag: Math的更多相关文章
- [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 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 ...
- 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 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 ...
- Java [Leetcode 172]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
给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...
- 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.. ...
随机推荐
- 小程序判断是否授权源码 auth.js
一.auth.js const configGlobal = require('../config/config_global.js'); var util = require('function.j ...
- jQuery属性操作(二)
挂载到$上的几个属性操作方法分析,发现属性操作用到了sizzle封装的方法 attr: function( elem, name, value ) { var hooks, ret, ...
- jQuery事件处理(一)
1.jQuery事件绑定的用法: $( "elem" ).on( events, [selector], [data], handler ); events:事件名称,可以是自定义 ...
- HTTP协议剖析 (附HttpWatch工具监控网络请求)
工具:HttpWatch Prov7.2.13破解版(带正版key) HTTP协议概述 思考2个要点: 第一:浏览器和服务器是通过什么连接的 第二:这种连接方式是怎么实现的 通过Interne ...
- java(6) ArrayList源码
系统环境: JDK 1.7 成员变量 //默认的初始化数组大小 private static final int DEFAULT_CAPACITY = 10; //空的对象数组 private sta ...
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
- 硬件RDMA的驱动配置和测试
author:headsen chen date: 2019-01-18 10:22:20 notice:created by headsen chen himself and not allow ...
- R子集subset
> x<-c(6,1,2,3,NA,12) > x[x>5] #x[5]是未知的,因此其值是否大于5也是未知的 [1] 6 NA 12 > subset(x,x& ...
- codeforces 782B - The Meeting Place Cannot Be Changed
time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standa ...
- NEFU 84 - 五指山 - [exgcd求解一元线性同余方程]
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=84 Time Limit:1000ms Memory Limit ...