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. Python 程序打包成 exe 可执行文件

    Python 程序打包工具 Python 是一个脚本语言,被解释器解释执行.它的发布方式: .py 文件:对于开源项目或者源码没那么重要的,直接提供源码,需要使用者自行安装 Python 并且安装依赖 ...

  2. [luogu 3773][CTSC 2017]吉夫特

     传送门  Solution 输入一个长度为n的数列,求有多少个长度大等于2的不上升子序列满足: \[\prod_{i=2}^{k} C(a_{b_{i-1}},a_{b_i}) mod\ 2 > ...

  3. 模板 - 数据结构 - 线段树/SegmentTree

    区间求加法和: 单点修改的,普通线段树. struct SegmentTree { #define ls (o<<1) #define rs (o<<1|1) static c ...

  4. 【Python 代码】生成hdf5文件

    import random from PIL import Image import numpy as np import os import h5py from PIL import Image L ...

  5. Django连接MySQL(二)

    1.首先我们需要创建好项目 2.安装MySQL数据库 3.setting中修改database设置 DATABASES = { 'default': { 'ENGINE': 'django.db.ba ...

  6. PostgreSQL远程连接,发生致命错误:没有用于主机“…”,用户“…”,数据库“…”,SSL关闭的pg_hba.conf记录

    PostgreSQL远程连接方法 有时候在远程连接时,会报Error connecting to the server:致命错误:没有用于主机“…”,用户“…”,数据库“…”,SSL关闭的pg_hba ...

  7. package.json 字段说明

    以vue的package.json为例: { // 名称 "name": "vue", // 版本 "version": "2.6 ...

  8. TextFX Notepad++

    textFx Notepad++ - 国内版 Bing https://cn.bing.com/search?FORM=U227DF&PC=U227&q=textFx+Notepad% ...

  9. Web Application Framework

    ASP.NET Boilerplate https://github.com/aspnetboilerplate ASP.NET Boilerplate - Web Application Frame ...

  10. 树莓派 more

    树莓派 rusthttps://tech.iotcomeon.com/2018/06/tech/deploy/515/sudo curl https://sh.rustup.rs -sSf | sh ...