LeetCode Factorial Trailing Zeroes
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/
题目:
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.
题解:
求factorial后结尾有多少个0, 就是求有多少个2和5的配对.
但是2比5多了很多,所以就是求5得个数。除此之外,还有一件事情要考虑。诸如25, 125之类的数字有不止一个5. e.g. n = 28, n!我们得到一个额外的5, 并且0的总数变成了6.
0 factorial 是 1. 不用单独考虑n == 0的情况.
n!后缀0的个数 = n!质因子中5的个数
= floor(n/5) + floor(n/25) + floor(n/125) + ....
Time Complexity: O(logn). Space: O(1).
AC Java:
public class Solution {
public int trailingZeroes(int n) {
int res = 0;
while(n>0){
res += n/5;
n/=5;
}
return res;
}
}
LeetCode Factorial Trailing Zeroes的更多相关文章
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
- [LeetCode] 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 O(logn)解法的理解
题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0 ...
- [LeetCode] Factorial Trailing Zeroes 阶乘末尾0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Python3解leetcode Factorial Trailing Zeroes
问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 ...
- LeetCode Factorial Trailing Zeroes (阶乘后缀零)
题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
随机推荐
- Java读取txt文件
package com.loongtao.general.crawler.slave.utils; import java.io.BufferedReader; import java.io.File ...
- Java正则表达式教程
地址:http://www.java3z.com/cwbwebhome/article/article8/Regex/Java.Regex.Tutorial.html#reg0_1
- winform学习之----图片控件应用(上一张,下一张)
示例1: int i = 0; string[] path = Directory.GetFiles(@"C:\Users\Administrator\Desktop\图片&q ...
- Asp反向代理程序,调用远程站点全站数据,一款脚本级反向代理程序.
前些天临时写的一脚本级反向代理程序,用法很简单,设置好目标站地址,然后放到你网站根目录:index.asp,再将404页面自定义为:index.asp,即可. 由于暂时没有 url 替换需要,所以没有 ...
- 数据库发出sql命令mysql教程
$db = mysql教程_connect("localhost", "phpdb", "phpdb");mysql_select_db(& ...
- IOS第六天(2:10秒倒计时)
****************10秒倒计时 #import "HMViewController.h" @interface HMViewController () <UIA ...
- 【iHMI43 4.3寸液晶模块】demo例程(版本1.00)发布
============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...
- Setting property 'source' to 'org.eclipse.jst.jee.server
警告: [SetPropertiesRule] Setting property 'source' to 'org.eclipse.jst.jee.server:project' did not fi ...
- ZOJ 2974 矩阵快速幂
题意 给出n个杯子与初始其中有多少水 “同时”进行如下指令 将其中的水同时分入所指定的杯子 进行x次后 输出杯子剩余水量 队友想出应该是一道快速幂 但并不是过去的用初始杯子的水组成的矩阵乘某个矩阵 可 ...
- Android通过网页打开App到指定页面并传递数据
首先在 Android Manifest 文件中注册 intent-filter <activity android:name=".MainActivity" android ...