172. 阶乘后的零

172. Factorial Trailing Zeroes

题目描述

给定一个整数 n,返回 n! 结果尾数中零的数量。

LeetCode172. Factorial Trailing Zeroes

示例 1:

输入: 3
输出: 0
解释: 3! = 6,尾数中没有零。

示例 2:

输入: 5
输出: 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;
}
}

参考资料

LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)的更多相关文章

  1. [Swift]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  2. Java实现 LeetCode 172 阶乘后的零

    172. 阶乘后的零 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! ...

  3. Leetcode 172.阶乘后的零

    阶乘后的零 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! = 120 ...

  4. 【每天一题】LeetCode 172. 阶乘后的零

    开源地址:点击该链接 题目描述 https://leetcode-cn.com/problems/factorial-trailing-zeroes 给定一个整数 n,返回 n! 结果尾数中零的数量. ...

  5. 172. 阶乘后的零 Java解法

    https://leetcode-cn.com/problems/factorial-trailing-zeroes/ 172. 阶乘后的零 这题要完成其实要知道一个很巧妙的思想,就是阶乘里面,后面的 ...

  6. leetcode刷题笔记172 阶乘后的零

    题目描述: 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例1: 输入: 输出: 解释: ! = , 尾数中没有零. 示例2: 输入: 输出: 解释: ! = , 尾数中有 个零. 说明: 你 ...

  7. 每日一道 LeetCode (41):阶乘后的零

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  8. 【LeetCode】172. Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

  9. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

随机推荐

  1. PHP ltrim() 函数

    例子 <?php $str = "Hello World!"; echo $str . "<br>"; echo ltrim($str,&qu ...

  2. nodejs 日志框架winston笔记

    winston是一款nodejs的日志库,本文以2.1.1版本为例,介绍一下使用方法. 1.基础用法 引用日志库,返回的是一个对象.包含一些构造器,实例方法. 其中transports是日志输出方式. ...

  3. 10.2.1.1-NAT+PAT综合详解

  4. IdHTTPServer开发https服务器

    IdHTTPServer开发https服务器 该篇经验同样适用于DATASNAP和UNIGUI,因为它们都基于INDY10. 1)需要TIdServerIOHandlerSSLOpenSSL控件 2) ...

  5. Mac和window生成ssh和查看ssh key

    一.MAC系统 mac 系统开始就已经为我们安装了ssh 如果没有安装,首先安装 打开终端:$ ssh -v 查看ssh版本 OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec ...

  6. GLTF模型查看器---优化器【转】

    https://blog.csdn.net/weixin_43081805/article/details/88743277 Clay Viewer(我只想说好用,直接可以导出gltf的二进制glb格 ...

  7. 常用的web api总结

    1.querySelector 获取指定元素中匹配css选择器的元素. // 作用在document document.querySelector("#nav"); // 获取文档 ...

  8. 封装qt http文件下载类

    #include <QApplication> #include <QtWidgets> #include <QtNetwork> //downloads one ...

  9. 文献阅读 - Genome-wide consequences of deleting any single gene

    现在在做单基因敲除后的转录组数据分析,一个显而易见却又百思不得其解的问题出现了: 为什么敲掉一个基因会导致其他那么多基因的表达发生了变化,甚至是其他很多调控基因(转录因子)? 我敲掉的只是一个普通基因 ...

  10. Javascript事件派发-dispatchEvent

    事件派发的作用: 1.派发数据,将一个封闭模块中的数据传递给另一个封闭模块.2.事件完成了较为复杂的解耦. 事件和回调函数不同在于: 1.事件可以在任意地方去获取,而回调函数只能在一个地方存在,如果需 ...