mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
添加 18 题
This commit is contained in:
@ -0,0 +1,10 @@
|
||||
package leetcode
|
||||
|
||||
func titleToNumber(s string) int {
|
||||
val, res := 0, 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
val = int(s[i] - 'A' + 1)
|
||||
res = res*26 + val
|
||||
}
|
||||
return res
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question171 struct {
|
||||
para171
|
||||
ans171
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para171 struct {
|
||||
s string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans171 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem171(t *testing.T) {
|
||||
|
||||
qs := []question171{
|
||||
|
||||
question171{
|
||||
para171{"A"},
|
||||
ans171{1},
|
||||
},
|
||||
|
||||
question171{
|
||||
para171{"AB"},
|
||||
ans171{28},
|
||||
},
|
||||
|
||||
question171{
|
||||
para171{"ZY"},
|
||||
ans171{701},
|
||||
},
|
||||
|
||||
question171{
|
||||
para171{"ABC"},
|
||||
ans171{731},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 171------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans171, q.para171
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, titleToNumber(p.s))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
67
leetcode/0171.Excel-Sheet-Column-Number/README.md
Normal file
67
leetcode/0171.Excel-Sheet-Column-Number/README.md
Normal file
@ -0,0 +1,67 @@
|
||||
# [171. Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a column title as appear in an Excel sheet, return its corresponding column number.
|
||||
|
||||
For example:
|
||||
|
||||
```
|
||||
A -> 1
|
||||
B -> 2
|
||||
C -> 3
|
||||
...
|
||||
Z -> 26
|
||||
AA -> 27
|
||||
AB -> 28
|
||||
...
|
||||
```
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: "A"
|
||||
Output: 1
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: "AB"
|
||||
Output: 28
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: "ZY"
|
||||
Output: 701
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个 Excel 表格中的列名称,返回其相应的列序号。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出 Excel 中列的名称,输出其对应的列序号。
|
||||
- 简单题。这一题是第 168 题的逆序题。按照 26 进制还原成十进制即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func titleToNumber(s string) int {
|
||||
val, res := 0, 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
val = int(s[i] - 'A' + 1)
|
||||
res = res*26 + val
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
```
|
Reference in New Issue
Block a user