[LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!.
Example 1:
- Input: 3
- Output: 0
- Explanation: 3! = 6, no trailing zero.
Example 2:
- Input: 5
- Output: 1
- Explanation: 5! = 120, one trailing zero.
Note: Your solution should be in logarithmic time complexity.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
这道题并没有什么难度,是让求一个数的阶乘末尾0的个数,也就是要找乘数中 10 的个数,而 10 可分解为2和5,而2的数量又远大于5的数量(比如1到 10 中有2个5,5个2),那么此题即便为找出5的个数。仍需注意的一点就是,像 25,125,这样的不只含有一个5的数字需要考虑进去,参加代码如下:
C++ 解法一:
- class Solution {
- public:
- int trailingZeroes(int n) {
- int res = ;
- while (n) {
- res += n / ;
- n /= ;
- }
- return res;
- }
- };
Java 解法一:
- public class Solution {
- public int trailingZeroes(int n) {
- int res = 0;
- while (n > 0) {
- res += n / 5;
- n /= 5;
- }
- return res;
- }
- }
这题还有递归的解法,思路和上面完全一样,写法更简洁了,一行搞定碉堡了。
C++ 解法二:
- class Solution {
- public:
- int trailingZeroes(int n) {
- return n == ? : n / + trailingZeroes(n / );
- }
- };
Java 解法二:
- public class Solution {
- public int trailingZeroes(int n) {
- return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
- }
- }
Github 同步地址:
https://github.com/grandyang/leetcode/issues/172
类似题目:
Preimage Size of Factorial Zeroes Function
参考资料:
https://leetcode.com/problems/factorial-trailing-zeroes/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数的更多相关文章
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数
LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...
- 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes
题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- 172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- Leetcode 172 Factorial Trailing Zeroes
给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...
随机推荐
- 送书『构建Apache Kafka流数据应用』和『小灰的算法之旅』和『Java并发编程的艺术』
读书好处 1.可以使我们增长见识. 2.可提高我们的阅读能力和写作水平. 3.可以使我们变的有修养. 4.可以使我们找到好工作. 5.可以使我们在竞争激烈的社会立于不败之地. 6.最大的好处是可以让你 ...
- 微信小程序:使用wx.request()请求后台接收不到参数
问题描述: 微信小程序:wx.request()请求后台接收不到参数,我通过wx.request()使用POST方式调用请求,参数传递不到后台 解决方案: Content-Type': 'applic ...
- argparse.ArgumentParser()用法解析
本博客主要本人学习记录用. 内容来源 于博客:https://blog.csdn.net/the_time_runner/article/details/97941409 argparse模块官方文档 ...
- CodeForces 574D Bear and Blocks
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n ...
- RESTful及API设计(原)
RESTful是一种架构风格,是由Fielding博士在自己的博士论文中提出并详细论述的. 它是用于指导web系统设计的,而指导API设计只是它的一小部分功能而已,如果只用它指导API设计就太大材小用 ...
- 三维网格细分算法(Catmull-Clark subdivision & Loop subdivision)附源码(转载)
转载: https://www.cnblogs.com/shushen/p/5251070.html 下图描述了细分的基本思想,每次细分都是在每条边上插入一个新的顶点,可以看到随着细分次数的增加,折 ...
- 百度编辑器UEditor,保存图片的配置问题
前言: 在使用百度编辑器UEditor的时候,如何将图片保存到服务器,我刚开始以为是要自己写上传文件的方法,后来发现只需要配置一下即可,如果你也正在使用百度富文本编辑器UEditor的话,这篇文章将非 ...
- 记录微信支付开发中的小经验(errcode = 40163; errmsg = "code been used")
今天上午客户提出问题,看了一下报错截图,应该是我更新版本时少传了一个参数,导致后续报错, 心里想着小问题,直接生产环境添加一下就行了,于是就为了我这一上午的悲剧埋下了伏笔 十分自信的把页面中的代码添加 ...
- Java内功心法,深入解析面向对象
什么是对象 对象是系统中用来描述客观事物的一个实体,它是构成系统的一个基本单位.一个对象由一组属性和对这组属性进行操作的一组服务组成. 类的实例化可生成对象,一个对象的生命周期包括三个阶段:生成.使用 ...
- python之滑动认证(图片)
from PIL import Image, ImageEnhance from io import BytesIO def cutImg(imgsrc): """ 根据 ...