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 ...
随机推荐
- jmeter分布式压测原理简介1
1.什么叫分布式压测? 分布式压测:模拟多台机器向目标机器产生压力,模拟几万用户并发访问 2.分布式压测原理:如下 3.更多补充.....待添加
- phpstrom php出现404
phpstrom搭配xampp用着很舒服,但是配置不小心容易出现404错误,解决并不难,几步就完成了 !第一步:依次选择Tools-Deployment-configuration进入后如果为空,点击 ...
- vue+elementui搭建后台管理界面(2首页)
1 会话存储 使用html5的 sessionStorage 对象临时保存会话 // 保存会话 sessionStorage.setItem('user', username) // 删除会话 ses ...
- html5中hgroup和address标签使用总结
html5中hgroup和address标签使用总结 一.总结 一句话总结: hgroup元素(不推荐使用):用来给标题分组,通常放在header中: address元素:斜体显示:用来说明作者的联系 ...
- NumPyArray
import arcpy import numpy # Create a simple array from scratch using random values myArray = numpy.r ...
- JMETER + POST + anti-forgery token
JMETER + POST + anti-forgery token Looking into XSRF/CSRF Prevention in ASP.NET MVC and Web Pages it ...
- openssl从内存中读取私钥进行签名
麻痹的找了好久,真恶心! #include <stdio.h> #include <stdlib.h> #ifdef WIN32 #include <windows.h& ...
- php foreach 无法改变数组的值的问题
转:http://www.cnblogs.com/yangwenxin/p/5845212.html 翻到PHP文档的foreach那页这样写道: “foreach 语法结构提供了遍历数组的简单方式. ...
- ntpd服务
yum -y install ntp 服务器端 [root@ip-172-31-6-148~]# vim /etc/ntp.conf ...# Use public servers from thep ...
- Java基础 Scanner 使用nextInt接收整数
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...