From 776b62b9b69a7132ba047809cd8c1b6504ede5a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Sat, 2 Apr 2022 16:57:14 +0800 Subject: [PATCH] =?UTF-8?q?420:=E5=BC=BA=E5=AF=86=E7=A0=81=E6=A3=80?= =?UTF-8?q?=E9=AA=8C=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/FactorialZerosLcci.java | 40 +++++++++++++++++++ .../cn/doc/content/FactorialZerosLcci.md | 16 ++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/FactorialZerosLcci.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/FactorialZerosLcci.md diff --git a/src/main/java/leetcode/editor/cn/FactorialZerosLcci.java b/src/main/java/leetcode/editor/cn/FactorialZerosLcci.java new file mode 100644 index 0000000..2625840 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/FactorialZerosLcci.java @@ -0,0 +1,40 @@ +//设计一个算法,算出 n 阶乘有多少个尾随零。 +// +// 示例 1: +// +// 输入: 3 +//输出: 0 +//解释: 3! = 6, 尾数中没有零。 +// +// 示例 2: +// +// 输入: 5 +//输出: 1 +//解释: 5! = 120, 尾数中有 1 个零. +// +// 说明: 你算法的时间复杂度应为 O(log n) 。 +// Related Topics 数学 👍 66 👎 0 + +package leetcode.editor.cn; + +//面试题 16.05:阶乘尾数 +public class FactorialZerosLcci { + public static void main(String[] args) { + Solution solution = new FactorialZerosLcci().new Solution(); + // TO TEST + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int trailingZeroes(int n) { + int count = 0; + while (n >= 5) { + count += n / 5; + n /= 5; + } + return count; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/FactorialZerosLcci.md b/src/main/java/leetcode/editor/cn/doc/content/FactorialZerosLcci.md new file mode 100644 index 0000000..a521799 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/FactorialZerosLcci.md @@ -0,0 +1,16 @@ +

设计一个算法,算出 n 阶乘有多少个尾随零。

+ +

示例 1:

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

示例 2:

+ +
输入: 5
+输出: 1
+解释: 5! = 120, 尾数中有 1 个零.
+ +

说明: 你算法的时间复杂度应为 O(log n) 

+
Related Topics
  • 数学

  • 👍 66
  • 👎 0
  • \ No newline at end of file