Skip to content

Commit a3e88e6

Browse files
committed
Add excel-sheet-column-number
1 parent 1e95f34 commit a3e88e6

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
|0146|[LRU缓存机制](https://leetcode-cn.com/problems/lru-cache/)|[go](./linkedList/146.LRUCache.go)|M|
4040
|0148|[排序链表](https://leetcode-cn.com/problems/sort-list/)|[go](./linkedList/148.sortList.go)|M|
4141
|0155|[最小栈](https://leetcode-cn.com/problems/min-stack/)|[go](./mystack/155.minStack.go)|S|
42+
|0171|[Excel表列序号](https://leetcode-cn.com/problems/excel-sheet-column-number/)|[go](./mystring/171.titleToNumber.go)|S|
4243
|0173|[二叉搜索树迭代器](https://leetcode-cn.com/problems/binary-search-tree-iterator/)|[go](./mystack/173.BSTIterator.go)|M|
4344
|0206|[反转链表](https://leetcode-cn.com/problems/reverse-linked-list/)|[go](./linkedList/206.reverseList.go)|S|
4445
|0208|[实现 Trie (前缀树)](https://leetcode-cn.com/problems/implement-trie-prefix-tree/)|[go](./trie/208.trie.go)|M|
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package mystring
2+
3+
func titleToNumber(s string) int {
4+
ans := 0
5+
for _, c := range s {
6+
num := c - 'A' + 1
7+
ans = ans*26 + int(num)
8+
}
9+
return ans
10+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package mystring
2+
3+
import (
4+
"testing"
5+
6+
"Leetcode/algorithms/kit"
7+
)
8+
9+
// go test -v -run Test_titleToNumber
10+
func Test_titleToNumber(t *testing.T) {
11+
cases := []kit.CaseEntry{
12+
{
13+
Name: "x1",
14+
Input: "A",
15+
Expected: 1,
16+
},
17+
{
18+
Name: "x2",
19+
Input: "AB",
20+
Expected: 28,
21+
},
22+
{
23+
Name: "x3",
24+
Input: "ZY",
25+
Expected: 701,
26+
},
27+
}
28+
29+
for _, tt := range cases {
30+
t.Run(tt.Name, func(t *testing.T) {
31+
if output := titleToNumber(tt.Input.(string)); output != tt.Expected.(int) {
32+
t.Errorf("titleToNumber(%v)=%d, expected=%v", tt.Input, output, tt.Expected)
33+
}
34+
})
35+
}
36+
}

0 commit comments

Comments
 (0)