Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 0955fc4

Browse files
aQuaaQua
aQua
authored and
aQua
committed
171 accepted. 6ms > 3ms
1 parent 88a9d30 commit 0955fc4

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# [171. Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/)
2+
3+
## 题目
4+
Related to question Excel Sheet Column Title
5+
Given a column title as appear in an Excel sheet, return its corresponding column number.
6+
7+
For example:
8+
```
9+
A -> 1
10+
B -> 2
11+
C -> 3
12+
...
13+
Z -> 26
14+
AA -> 27
15+
AB -> 28
16+
```
17+
18+
## 解题思路
19+
20+
见程序注释
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Problem0171
2+
3+
func titleToNumber(s string) int {
4+
res := 0
5+
6+
for i := 0; i < len(s); i++ {
7+
temp := int(s[i] - 'A' + 1)
8+
res = res*26 + temp
9+
}
10+
11+
return res
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Problem0171
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
s string
13+
ans int
14+
}{
15+
16+
{
17+
"AA",
18+
27,
19+
},
20+
21+
// 可以有多个 testcase
22+
}
23+
24+
func Test_titleToNumber(t *testing.T) {
25+
ast := assert.New(t)
26+
27+
for _, tc := range tcs {
28+
fmt.Printf("~~%v~~\n", tc)
29+
ast.Equal(tc.ans, titleToNumber(tc.s), "输入:%v", tc)
30+
}
31+
}
32+
33+
func Benchmark_titleToNumber(b *testing.B) {
34+
for i := 0; i < b.N; i++ {
35+
for _, tc := range tcs {
36+
titleToNumber(tc.s)
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)