LeetCode(172) Factorial Trailing Zeroes
题目如下:
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相关推荐
- LeetCode 172. Factorial Trailing Zeroes
LeetCode 172. Factorial Trailing Zeroes 问题来源LeetCode 172. Factorial Trailing Zeroes 问题描述 Given an in ...
- Leetcode 172 Factorial Trailing Zeroes
1.题目要求 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)
数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...
- 172. Factorial Trailing Zeroes
/**172. Factorial Trailing Zeroes *2016-6-4 by Mingyang* 首先别忘了什么是factorial,就是阶乘.那么很容易想到需要统计* (2,5)对的 ...
- leetcode python3 简单题172. Factorial Trailing Zeroes
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十二题 (1)题目 英文: Given an integer n, retu ...
- 【LeetCode】172 - Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 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 ...
- 172 Factorial Trailing Zeroes(阶乘后的零)————附带详细思路和代码
文章目录 0 效果 1 题目 2 思路 3 代码 0 效果 1 题目 Given an integer n, return the number of trailing zeroes in n!. N ...
- 【leetcode❤python】172. Factorial Trailing Zeroes
#-*- coding: UTF-8 -*- #给定一个整数N,那么N的阶乘N!末尾有多少个0? 比如:N=10,N!=3628800,N!的末尾有2个0. #所有的尾部的0可以看做都是2*5得来的, ...
最新文章
- CStatic 控件设置文本,不能重回问题
- numpy.tril详解
- Java整合Spring发送邮件
- .NET Core UI框架Avalonia
- 《当程序员的那些狗日日子》(三十六)无名的配角
- Java-Tomcat 5.5 下的JNDI
- python进阶08并发之四map, apply, map_async, apply_async差异
- java 停止定时器_实例助解java定时器设置及停止的方法
- 最小二乘法曲线拟合以及Matlab实现-----实验结果资源和总结
- python爬裁判文书网_记录用web scraper爬取裁判文书网的文书列表信息以及批量下载word文书...
- 自适应simpson积分
- http和https的区别
- MySQL高级(一)
- 喜大普奔!Maya 2022来了?!
- 工业相机——选型及参数
- linux系统怎么远程进服务器
- access2003不能启动修复资源
- 其它服务器(ThinkPHP5)与Discuz3.3自带的UCenter实现同步(一) - 通信成功
- 计算机语言输入法,win7输入法不见了要怎么调出来【详解】
- 数学在计算机专业中的应用,高等数学在计算机专业教学中的应用