规范格式

This commit is contained in:
YDZ
2020-08-07 15:50:06 +08:00
parent 854a339abc
commit 4e11f4028a
1438 changed files with 907 additions and 924 deletions

View File

@ -0,0 +1,8 @@
package leetcode
func trailingZeroes(n int) int {
if n/5 == 0 {
return 0
}
return n/5 + trailingZeroes(n/5)
}

View File

@ -0,0 +1,47 @@
package leetcode
import (
"fmt"
"testing"
)
type question172 struct {
para172
ans172
}
// para 是参数
// one 代表第一个参数
type para172 struct {
s int
}
// ans 是答案
// one 代表第一个答案
type ans172 struct {
one int
}
func Test_Problem172(t *testing.T) {
qs := []question172{
question172{
para172{3},
ans172{0},
},
question172{
para172{5},
ans172{1},
},
}
fmt.Printf("------------------------Leetcode Problem 172------------------------\n")
for _, q := range qs {
_, p := q.ans172, q.para172
fmt.Printf("【input】:%v 【output】:%v\n", p, trailingZeroes(p.s))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,35 @@
# [172. Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/)
## 题目:
Given an integer n, return the number of trailing zeroes in n!.
**Example 1:**
Input: 3
Output: 0
Explanation: 3! = 6, no trailing zero.
**Example 2:**
Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.
**Note:** Your solution should be in logarithmic time complexity.
## 题目大意
给定一个整数 n返回 n! 结果尾数中零的数量。说明: 你算法的时间复杂度应为 O(log n) 。
## 解题思路
- 给出一个数 n要求 n末尾 0 的个数。
- 这是一道数学题。计算 N 的阶乘有多少个后缀 0即计算 N! 里有多少个 10也是计算 N! 里有多少个 2 和 5分解质因数最后结果即 2 的个数和 5 的个数取较小值。每两个数字就会多一个质因数 2而每五个数字才多一个质因数 5。每 5 个数字就会多一个质因数 5。0~4 的阶乘里没有质因数 55~9 的阶乘里有 1 个质因数 510~14 的阶乘里有 2 个质因数 5依此类推。所以 0 的个数即为 `min(阶乘中 5 的个数和 2 的个数)`
- N! 有多少个后缀 0即 N! 有多少个质因数 5。N! 有多少个质因数 5即 N 可以划分成多少组 5个数字一组加上划分成多少组 25 个数字一组,加上划分多少组成 125 个数字一组,等等。即 `res = N/5 + N/(5^2) + N/(5^3) + ... = ((N / 5) / 5) / 5 /...` 。最终算法复杂度为 O(logN)。