LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零
172. Factorial Trailing Zeroes
题目描述
给定一个整数 n,返回 n! 结果尾数中零的数量。
LeetCode172. Factorial Trailing Zeroes
示例 1:
输出: 0
解释: 3! = 6,尾数中没有零。
示例 2:
输出: 1
解释: 5! = 120,尾数中有 1 个零。
说明: 你算法的时间复杂度应为 O(log n)。
Java 实现
递归
class Solution {
public static int trailingZeroes(int n) {
return n < 5 ? 0 : n / 5 + trailingZeroes(n / 5);
}
}
迭代
class Solution {
public static int trailingZeroes(int n) {
int result = 0;
while (n > 0) {
result += n / 5;
n /= 5;
}
return result;
}
}
参考资料
- https://leetcode.com/problems/factorial-trailing-zeroes/
- https://leetcode-cn.com/problems/factorial-trailing-zeroes/
LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)的更多相关文章
- [Swift]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- Java实现 LeetCode 172 阶乘后的零
172. 阶乘后的零 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! ...
- Leetcode 172.阶乘后的零
阶乘后的零 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! = 120 ...
- 【每天一题】LeetCode 172. 阶乘后的零
开源地址:点击该链接 题目描述 https://leetcode-cn.com/problems/factorial-trailing-zeroes 给定一个整数 n,返回 n! 结果尾数中零的数量. ...
- 172. 阶乘后的零 Java解法
https://leetcode-cn.com/problems/factorial-trailing-zeroes/ 172. 阶乘后的零 这题要完成其实要知道一个很巧妙的思想,就是阶乘里面,后面的 ...
- leetcode刷题笔记172 阶乘后的零
题目描述: 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例1: 输入: 输出: 解释: ! = , 尾数中没有零. 示例2: 输入: 输出: 解释: ! = , 尾数中有 个零. 说明: 你 ...
- 每日一道 LeetCode (41):阶乘后的零
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
随机推荐
- Flask一种通用视图,增删改查RESTful API的设计
模型设计是后端开发的第一步.数据模型反映了各种对象之间的相互关系. from app import db class Role(db.Model): """角色" ...
- Compiling OpenCV: VTK Not Found on Ubuntu 16.04 LTS
When installing OpenCV: /usr/bin/vtk not found libvtkRenderingPythonTkWidgets.so not found /usr/bin/ ...
- GO语言strconv包的使用
Go语言中strconv包实现了基本数据类型和其字符串表示的相互转换. strconv包 strconv包实现了基本数据类型与其字符串表示的转换,官方文档中文版. string与int类型转换 Ato ...
- Simplifying Failures
# # Finish the delta debug function ddmin # import re def test(s): print s, len(s),repr(s) if re.sea ...
- CUDA编程之快速入门【转】
https://www.cnblogs.com/skyfsm/p/9673960.html CUDA(Compute Unified Device Architecture)的中文全称为计算统一设备架 ...
- useRef获取DOM元素和保存变量(十)
useRef在工作中虽然用的不多,但是也不能缺少.它有两个主要的作用: 用useRef获取React JSX中的DOM元素,获取后你就可以控制DOM的任何东西了.但是一般不建议这样来作,React界面 ...
- win10安装Navicat 12 for MySQL
Navicat 下载地址: https://blog.csdn.net/u013600314/article/details/80605981 Navicat 连接Mysql 的方法:https:// ...
- numpy linspace
https://www.cnblogs.com/antflow/p/7220798.html numpy.linspace(start, stop, num=50, endpoint=True, re ...
- Kotlin集合——Set集合
Kotlin集合——Set集合 转 https://www.jianshu.com/p/3c95d7729d69 Kotlin的集合类由两个接口派生:Collection和Map. Kotlin的 ...
- 【转】项目搬迁,快捷导出环境依赖包到requirements.txt
项目搬迁的时候,需要把当前的环境依赖包导出,然后到部署项目的服务器上安装依赖. 我们可以通过下面的命令执行,把依赖包导出到requirements.txt文件里. 生成requirements.txt ...