1、题目要求

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

Note: Your solution should be in logarithmic time complexity.

题目意思是求n的阶乘后面末尾0的个数,并且时间复杂度是对数级别。

2、分析

一个数 n 的阶乘末尾有多少个 0 取决于从 1 到 n 的各个数的因子中 2 和 5 的个数, 而 2 的个数是远远多余 5 的个数的, 因此求出 5 的个数即可. 题解中给出的求解因子 5 的个数的方法是用 n 不断除以 5, 直到结果为 0, 然后把中间得到的结果累加. 例如, 100/5 = 20, 20/5 = 4, 4/5 = 0, 则 1 到 100 中因子 5 的个数为 (20 + 4 + 0) = 24 个, 即 100 的阶乘末尾有 24 个 0. 其实不断除以 5, 是因为每间隔 5 个数有一个数可以被 5 整除, 然后在这些可被 5 整除的数中, 每间隔 5 个数又有一个可以被 25 整除, 故要再除一次, ... 直到结果为 0, 表示没有能继续被 5 整除的数了.

代码如下:

 1 class Solution {
 2 public:
 3     int trailingZeroes(int n)
 4     {
 5         int counter=0;  //the counter!
 6         int k=n;
 7
 8          while(k>=5)
 9          {
10             k=k/5;
11             counter+=k;
12          }
13
14         return counter;
15     }
16 };

转载于:https://www.cnblogs.com/LCCRNblog/p/4392402.html

Leetcode 172 Factorial Trailing Zeroes相关推荐

  1. LeetCode 172. Factorial Trailing Zeroes

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

  2. LeetCode(172) Factorial Trailing Zeroes

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

  3. leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)

    数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...

  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. Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]

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

  8. 172 Factorial Trailing Zeroes(阶乘后的零)————附带详细思路和代码

    文章目录 0 效果 1 题目 2 思路 3 代码 0 效果 1 题目 Given an integer n, return the number of trailing zeroes in n!. N ...

  9. 【leetcode❤python】172. Factorial Trailing Zeroes

    #-*- coding: UTF-8 -*- #给定一个整数N,那么N的阶乘N!末尾有多少个0? 比如:N=10,N!=3628800,N!的末尾有2个0. #所有的尾部的0可以看做都是2*5得来的, ...

最新文章

  1. PacBio sequence error correction amd assemble via pacBioToCA
  2. iPhone开发:Objective-c中@property声明时的参数释疑
  3. 机器人车间气管_汽车消声器生产中的焊接机器人应用与工艺流程
  4. 使用intelliJ创建 spring boot + gradle + mybatis站点
  5. [codevs 1302] 小矮人(2002年CEOI中欧信息学奥赛)
  6. php 递归展现城市信息,PHP 递归兑现层级树状展现数据
  7. inittab文件格式
  8. 变量:2021数字科技前沿应用趋势
  9. selenium headless报错Message: unknown error: failed to wait for extension background page to load
  10. 生成内核版本号头文件的方法
  11. 壁纸控:小清新桌面壁纸
  12. QT中ui更改后不能更新的解决方法
  13. HttpClient FormUrlEncodedContent System.UriFormatException: 无效的 URI: URI 字符串太长问题解决方案
  14. linux fdisk 4k,linux查看硬盘4K对齐方法
  15. 【JavaScript】回流(reflow)与重绘(repaint)
  16. HDU4035 Maze 【树形DP】【期望DP】
  17. edius隐藏快捷键_edius8常用快捷键有哪些|edius快捷键使用大全汇总 - 软件教程 - 格子啦...
  18. 【新书推荐】【2021】基于多源信息融合的航天器自主导航技术
  19. Windows启动过程详解
  20. 销售书籍推荐,这本书做销售的必看!

热门文章

  1. java阻塞锁_java – 阻止锁与非阻塞锁
  2. python 子线程返回值_python-从线程返回值
  3. 挑战记忆力-Web前端实现记忆纸牌游戏(JS+CSS)
  4. Oracle12c部署,允许远程访问
  5. c语言文件读写_学生信息管理系统(C语言\单向链表\文件读写)
  6. delay 芯片时序output_【第二章 STA概念 上】静态时序分析圣经翻译计划
  7. 外部中断实验 编写程序学习外部中断的电平触发方式。无中断时发光让发光二极管从左到右依次点亮,有外部中断请求时,4位数码管从0000开始加1显示(加到9999后复位为0000),同时蜂鸣器报警。
  8. 在线js拼接html代码,关于js拼接html元素?
  9. java 原子类能做什么_死磕 java原子类之终结篇(面试题)
  10. 如何在Node.js的httpServer中接收前端发送的arraybuffer数据