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;}

转载于:https://www.cnblogs.com/little-YTMM/p/4513224.html

LeetCode--Factorial Trailing Zeroes(注意)相关推荐

  1. [LeetCode] Factorial Trailing Zeroes

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

  2. LeetCode Factorial Trailing Zeroes(计算阶乘结果尾部有几个0)

    思路:计算n能被5,25,...b整除的个数 代码如下: public class Solution {public int trailingZeroes(int n){int sum = 0;whi ...

  3. LeetCode 172. Factorial Trailing Zeroes

    LeetCode 172. Factorial Trailing Zeroes 问题来源LeetCode 172. Factorial Trailing Zeroes 问题描述 Given an in ...

  4. 172. Factorial Trailing Zeroes

    /**172. Factorial Trailing Zeroes *2016-6-4 by Mingyang* 首先别忘了什么是factorial,就是阶乘.那么很容易想到需要统计* (2,5)对的 ...

  5. leetcode python3 简单题172. Factorial Trailing Zeroes

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十二题 (1)题目 英文: Given an integer n, retu ...

  6. 【LeetCode】172 - Factorial Trailing Zeroes

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

  7. Leetcode 172 Factorial Trailing Zeroes

    1.题目要求 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  8. C#LeetCode刷题之#172-阶乘后的零(Factorial Trailing Zeroes)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3854 访问. 给定一个整数 n,返回 n! 结果尾数中零的数量. ...

  9. LeetCode(172) Factorial Trailing Zeroes

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

  10. Factorial Trailing Zeroes

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

最新文章

  1. softmax layer 简单理解以及实际例子【有白话讲解】
  2. (转载)你所知晓的dt.jar
  3. IT培训课、视频教程和书本之PK
  4. 注册登录案例用MVC和mysql_用MVC模式实现简单用户登录注册功能
  5. Windows 10 中 VMware 要求禁用 Device Guard 问题
  6. 程序员修神之路--缓存架构不够好,系统容易瘫痪
  7. 第23天:js-数据类型转换
  8. asp.net很有用的字符串操作类
  9. windows docker 设置镜像源_Windows10下搭建第一个Docker应用(Demo)
  10. 【北京集训D2T3】tvt
  11. WEB前端开发规范文档示例
  12. unity旗帜飘动shader
  13. C盘瘦身:QQ文件的清理及Group2文件夹
  14. C语言 —— pta寻找孪生素数 (20 分)
  15. 【博客418】cpu平坦模型和分段模型
  16. php获取目录下所有文件及目录(多种方法)
  17. 如何禁用计算机防病毒程序,win10系统怎么关闭Windows Defender防病毒程序
  18. JavaScript专精系列(6)——FileReader 文件读取
  19. AAMA 2604 铝型材和板材上的高性能有机涂层的推荐规范、性能要求和测试方法
  20. Unity 判断格子是否在封闭空间内(房间中)

热门文章

  1. 【Linux】一步一步学Linux——visudo命令(104)
  2. 80端口攻击_内网端口转发工具的使用总结
  3. c语言图片见水印,[求助]C语言 bmp文件加上水印
  4. on java 8 学习笔记 2022.2.16
  5. NCHW与NC4HW4数据排布在卷积优化上的优劣分析
  6. iTunes 降级安装 12.6
  7. AppleScript 快速入门
  8. c++ 中this 和 *this区别
  9. mysql如何explan优化sql_《MySQL数据库》MySQL 优化SQL(explain)
  10. 计组-数据通路的功能和基本结构