Given an integer n, return the number of trailing zeroes in n!.

问题描述:给出一个正整数n,计算n!结构后面有几个0.要求:在多项式时间中完成算法。

常规思路:计算n!,然后统计一下后面有几个0,但是这种算法一想就知道肯定会超出时间限制。

巧妙思路:相乘得0,则只有2*5相乘能得到0;而0的个数也只会与2、5的个数有关,而一个数一定能分解成1^x1+2^x2+3^x3+5^x5+7^x7+……的形式,而且2的幂方数一定比5的幂方数多;

2*5=10;

2*5*2*5=100;

即有几个5一定会有几个2与之配套,有几套2-5,则便会有几个零。

代码如下:

public int trailingZeroes(int n) {
int count = 0;
for( ; n/5>=1;){
count = count + n/5;
n = n/5;
}
return count;
}

LeetCode--Factorial Trailing Zeroes(注意)的更多相关文章

  1. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

  2. [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  3. LeetCode Factorial Trailing Zeroes

    原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...

  4. 关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解

    题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0 ...

  5. [LeetCode] Factorial Trailing Zeroes 阶乘末尾0

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  6. Python3解leetcode Factorial Trailing Zeroes

    问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 ...

  7. LeetCode Factorial Trailing Zeroes (阶乘后缀零)

    题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...

  8. LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)

    172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...

  9. 【LeetCode】172. Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

  10. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

随机推荐

  1. CSS基础全荟

    一.CSS概述 1.css是什么?? 层叠样式表 2.css的引入方式 1.行内样式   在标签上加属性style="属性名1:属性值1;属性名2:属性值2;..." 2.内嵌式  ...

  2. Git配置技巧及常用命令总结

    如果你想精通Git,直接到 Git官网 把这本ProGit掌握已足以Pro Git 配置用户信息 user和email,--global参数全局配置,当然你也可以不加此参数,不同的项目用不同的用户名和 ...

  3. Python 一些好玩的函数

    一.匿名函数 什么匿名是函数: 不需要使用def函数名的函数或者子程序 函数语法: lambda 参数:表达式 函数特点: 1.lambda只是一个表达式,省去定义函数过程,让代码更精简 2.lamb ...

  4. Asp.NET Core 在IIS部署 An assembly specified in the application dependencies manifest was not found

    今天在发布应用的时候,出来了一个报错:An assembly specified in the application dependencies manifest was not found 情况如下 ...

  5. php-语言参考-类型3.2-未完待续

    一,PHP变量的8个类型 四种标量类型: boolean (布尔型) integer (整型) float (浮点型, 也称作 double) string (字符串) 两种复合类型: array ( ...

  6. 洛谷U32670 小凯的数字(比赛)

    题目网址 https://www.luogu.org/problemnew/show/U32670 题目背景 NOIP2018 原创模拟题T1 NOIP DAY1 T1 or DAY 2 T1 难度 ...

  7. 005---基于UDP的套接字

    基于UDP的套接字 udp不同于tcp协议:不需要经过三次握手.四次挥手.直接发送数据就行. 服务端 import socket ip_port = ('127.0.0.1', 8001) buffe ...

  8. 线程基础三 使用C#中的lock关键词

    C#中lock关键字主要是为确保当一个线程使用某些资源时,同时无法其他线程无法使用该资源.下面我们看看下面的小例子. static void Main(string[] args) { var c = ...

  9. React 省市区三级联动

    省市区所对应的数据来自:http://www.zgguan.com/zsfx/jsjc/6541.html react中的代码是: export default class AddReceive ex ...

  10. 6.JAVA知识点归纳整理

    一.jdk初识与HelloWord: 二.java基础: 2.1 标识符_关键字_数据类型 2.2 数据类型转换 2.3 程序编写格式 2.4 运算符 2.5 分支与for循环 2.6 while_b ...