mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
规范格式
This commit is contained in:
35
leetcode/0483.Smallest-Good-Base/483. Smallest Good Base.go
Normal file
35
leetcode/0483.Smallest-Good-Base/483. Smallest Good Base.go
Normal file
@ -0,0 +1,35 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func smallestGoodBase(n string) string {
|
||||
num, _ := strconv.ParseUint(n, 10, 64)
|
||||
for bit := uint64(math.Log2(float64(num))); bit >= 1; bit-- {
|
||||
low, high := uint64(2), uint64(math.Pow(float64(num), 1.0/float64(bit)))
|
||||
for low < high {
|
||||
mid := uint64(low + (high-low)>>1)
|
||||
sum := findBase(mid, bit)
|
||||
if sum == num {
|
||||
return strconv.FormatUint(mid, 10)
|
||||
} else if sum > num {
|
||||
high = mid - 1
|
||||
} else {
|
||||
low = mid + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return strconv.FormatUint(num-1, 10)
|
||||
}
|
||||
|
||||
// 计算 k^m + k^(m-1) + ... + k + 1
|
||||
func findBase(mid, bit uint64) uint64 {
|
||||
sum, base := uint64(1), uint64(1)
|
||||
for i := uint64(1); i <= bit; i++ {
|
||||
base *= mid
|
||||
sum += base
|
||||
}
|
||||
return sum
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question483 struct {
|
||||
para483
|
||||
ans483
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para483 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans483 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
func Test_Problem483(t *testing.T) {
|
||||
|
||||
qs := []question483{
|
||||
|
||||
question483{
|
||||
para483{"13"},
|
||||
ans483{"3"},
|
||||
},
|
||||
|
||||
question483{
|
||||
para483{"4681"},
|
||||
ans483{"8"},
|
||||
},
|
||||
|
||||
question483{
|
||||
para483{"1000000000000000000"},
|
||||
ans483{"999999999999999999"},
|
||||
},
|
||||
|
||||
question483{
|
||||
para483{"727004545306745403"},
|
||||
ans483{"727004545306745402"},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 483------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans483, q.para483
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, smallestGoodBase(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
95
leetcode/0483.Smallest-Good-Base/README.md
Executable file
95
leetcode/0483.Smallest-Good-Base/README.md
Executable file
@ -0,0 +1,95 @@
|
||||
# [483. Smallest Good Base](https://leetcode.com/problems/smallest-good-base/)
|
||||
|
||||
|
||||
## 题目:
|
||||
|
||||
For an integer n, we call k>=2 a **good base** of n, if all digits of n base k are 1.
|
||||
|
||||
Now given a string representing n, you should return the smallest good base of n in string format.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: "13"
|
||||
Output: "3"
|
||||
Explanation: 13 base 3 is 111.
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input: "4681"
|
||||
Output: "8"
|
||||
Explanation: 4681 base 8 is 11111.
|
||||
|
||||
**Example 3:**
|
||||
|
||||
Input: "1000000000000000000"
|
||||
Output: "999999999999999999"
|
||||
Explanation: 1000000000000000000 base 999999999999999999 is 11.
|
||||
|
||||
**Note:**
|
||||
|
||||
1. The range of n is [3, 10^18].
|
||||
2. The string representing n is always valid and will not have leading zeros.
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
|
||||
对于给定的整数 n, 如果n的k(k>=2)进制数的所有数位全为1,则称 k(k>=2)是 n 的一个好进制。
|
||||
|
||||
以字符串的形式给出 n, 以字符串的形式返回 n 的最小好进制。
|
||||
|
||||
提示:
|
||||
|
||||
- n 的取值范围是 [3, 10^18]。
|
||||
- 输入总是有效且没有前导 0。
|
||||
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
|
||||
- 给出一个数 n,要求找一个进制 k,使得数字 n 在 k 进制下每一位都是 1 。求最小的进制 k。
|
||||
- 这一题等价于求最小的正整数 k,满足存在一个正整数 m 使得
|
||||
|
||||
<p align='center'>
|
||||
<img src='https://img.halfrost.com/Leetcode/leetcode_483_1.png'>
|
||||
</p>
|
||||
|
||||
|
||||
- 这一题需要确定 k 和 m 两个数的值。m 和 k 是有关系的,确定了一个值,另外一个值也确定了。由
|
||||
|
||||
<p align='center'>
|
||||
<img src='https://img.halfrost.com/Leetcode/leetcode_483_2.png'>
|
||||
</p>
|
||||
|
||||
|
||||
可得:
|
||||
|
||||
<p align='center'>
|
||||
<img src='https://img.halfrost.com/Leetcode/leetcode_483_3.png'>
|
||||
</p>
|
||||
|
||||
|
||||
根据题意,可以知道 k ≥2,m ≥1 ,所以有:
|
||||
|
||||
<p align='center'>
|
||||
<img src='https://img.halfrost.com/Leetcode/leetcode_483_4.png'>
|
||||
</p>
|
||||
|
||||
|
||||
所以 m 的取值范围确定了。那么外层循环从 1 到 log n 遍历。找到一个最小的 k ,能满足:
|
||||
|
||||
可以用二分搜索来逼近找到最小的 k。先找到 k 的取值范围。由
|
||||
|
||||
<p align='center'>
|
||||
<img src='https://img.halfrost.com/Leetcode/leetcode_483_5.png'>
|
||||
</p>
|
||||
|
||||
|
||||
可得,
|
||||
|
||||
<p align='center'>
|
||||
<img src='https://img.halfrost.com/Leetcode/leetcode_483_6.png'>
|
||||
</p>
|
||||
|
||||
所以 k 的取值范围是 [2, n*(1/m) ]。再利用二分搜索逼近找到最小的 k 即为答案。
|
Reference in New Issue
Block a user