添加 18 题

This commit is contained in:
YDZ
2020-08-12 20:11:32 +08:00
parent e5bd520fdd
commit d0cda0c904
72 changed files with 4008 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package leetcode
func convertToTitle(n int) string {
result := []byte{}
for n > 0 {
result = append(result, 'A'+byte((n-1)%26))
n = (n - 1) / 26
}
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
result[i], result[j] = result[j], result[i]
}
return string(result)
}

View File

@ -0,0 +1,67 @@
package leetcode
import (
"fmt"
"testing"
)
type question168 struct {
para168
ans168
}
// para 是参数
// one 代表第一个参数
type para168 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans168 struct {
one string
}
func Test_Problem168(t *testing.T) {
qs := []question168{
question168{
para168{1},
ans168{"A"},
},
question168{
para168{28},
ans168{"AB"},
},
question168{
para168{701},
ans168{"ZY"},
},
question168{
para168{10011},
ans168{"NUA"},
},
question168{
para168{999},
ans168{"ALK"},
},
question168{
para168{681},
ans168{"ZE"},
},
}
fmt.Printf("------------------------Leetcode Problem 168------------------------\n")
for _, q := range qs {
_, p := q.ans168, q.para168
fmt.Printf("【input】:%v 【output】:%v\n", p, convertToTitle(p.n))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,80 @@
# [168. Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/)
## 题目
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
```
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
...
```
**Example 1**:
```
Input: 1
Output: "A"
```
**Example 2**:
```
Input: 28
Output: "AB"
```
**Example 3**:
```
Input: 701
Output: "ZY"
```
## 题目大意
给定一个正整数,返回它在 Excel 表中相对应的列名称。
例如,
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
...
## 解题思路
- 给定一个正整数,返回它在 Excel 表中的对应的列名称
- 简单题。这一题就类似短除法的计算过程。以 26 进制的字母编码。按照短除法先除,然后余数逆序输出即可。
## 代码
```go
package leetcode
func convertToTitle(n int) string {
result := []byte{}
for n > 0 {
result = append(result, 'A'+byte((n-1)%26))
n = (n - 1) / 26
}
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
result[i], result[j] = result[j], result[i]
}
return string(result)
}
```