题目如下:

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

分析如下:

看上去简单,但是写出logarithmic time complexity的代码还是需要一些思考的。分析在下面的代码注释中。

我的代码:

// 88ms
class Solution {
public:int trailingZeroes(int n) {//计算包含的2和5组成的pair的个数就可以了,一开始想错了,还算了包含的10的个数。//因为5的个数比2少,所以2和5组成的pair的个数由5的个数决定。//观察15! = 有3个5(来自其中的5, 10, 15), 所以计算n/5就可以。//但是25! = 有6个5(有5个5来自其中的5, 10, 15, 20, 25, 另外还有1个5来自25=(5*5)的另外一个5),//所以除了计算n/5, 还要计算n/5/5, n/5/5/5, n/5/5/5/5, ..., n/5/5/5,,,/5直到商为0。int count_five = 0;while ( n > 0) {int k = n / 5;count_five += k;n = k;}return count_five;}
};/*
// TLE版本
class Solution {
public:int trailingZeroes(int n) {//计算包含的2和5组成的pair的个数就可以了,一开始想错了,还算了包含的10的个数。//因为5的个数比2少,所以2和5组成的pair的个数由5的个数决定。//观察15! = 有3个5(来自其中的5, 10, 15), 所以计算n / 15就可以。//但是25! = 有6个5(有5个5来自其中的5, 10, 15, 20, 25, 另外还有1个5来自25的另外一个5),//所以除了计算n / 15, 还要计算n/15/15/../15直到商为0。int count_five = 0;for (int i = 1; i <=n; ++i) {int current = i;while (current%5 == 0) {current /= 5;count_five++;}}return count_five;}
};
*/

对 时间复杂度的名词解释备忘在这里。

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

    1.题目要求 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. CStatic 控件设置文本,不能重回问题
  2. numpy.tril详解
  3. Java整合Spring发送邮件
  4. .NET Core UI框架Avalonia
  5. 《当程序员的那些狗日日子》(三十六)无名的配角
  6. Java-Tomcat 5.5 下的JNDI
  7. python进阶08并发之四map, apply, map_async, apply_async差异
  8. java 停止定时器_实例助解java定时器设置及停止的方法
  9. 最小二乘法曲线拟合以及Matlab实现-----实验结果资源和总结
  10. python爬裁判文书网_记录用web scraper爬取裁判文书网的文书列表信息以及批量下载word文书...
  11. 自适应simpson积分
  12. http和https的区别
  13. MySQL高级(一)
  14. 喜大普奔!Maya 2022来了?!
  15. 工业相机——选型及参数
  16. linux系统怎么远程进服务器
  17. access2003不能启动修复资源
  18. 其它服务器(ThinkPHP5)与Discuz3.3自带的UCenter实现同步(一) - 通信成功
  19. 计算机语言输入法,win7输入法不见了要怎么调出来【详解】
  20. 数学在计算机专业中的应用,高等数学在计算机专业教学中的应用

热门文章

  1. 如何计算两个坐标点的方位角
  2. Jsp+mysql在线考试系统论文
  3. 栈和队列的定义、特点和用途
  4. 首届“十大最具价值”AR/VR创业项目遴选榜单丨Xtecher权威发布
  5. 2017云栖大会门票转让_2017云栖大会门票多少钱?
  6. Android 上传代码到gitee并发布到Jitpack生成远程库
  7. Redis基本操作命令(图文详解)
  8. 翟天临之后,大连博士细数区块链博士圈那些事……
  9. vivos9设置定时开关机方法分享
  10. android的文字识别OCR