-
Notifications
You must be signed in to change notification settings - Fork 0
/
168.excel表列名称.go
87 lines (81 loc) · 1.12 KB
/
168.excel表列名称.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* @lc app=leetcode.cn id=168 lang=golang
*
* [168] Excel表列名称
*
* https://leetcode-cn.com/problems/excel-sheet-column-title/description/
*
* algorithms
* Easy (43.46%)
* Likes: 520
* Dislikes: 0
* Total Accepted: 103.5K
* Total Submissions: 237.7K
* Testcase Example: '1'
*
* 给你一个整数 columnNumber ,返回它在 Excel 表中相对应的列名称。
*
* 例如:
*
*
* A -> 1
* B -> 2
* C -> 3
* ...
* Z -> 26
* AA -> 27
* AB -> 28
* ...
*
*
*
*
* 示例 1:
*
*
* 输入:columnNumber = 1
* 输出:"A"
*
*
* 示例 2:
*
*
* 输入:columnNumber = 28
* 输出:"AB"
*
*
* 示例 3:
*
*
* 输入:columnNumber = 701
* 输出:"ZY"
*
*
* 示例 4:
*
*
* 输入:columnNumber = 2147483647
* 输出:"FXSHRXW"
*
*
*
*
* 提示:
*
*
* 1
*
*
*/
// @lc code=start
func convertToTitle(columnNumber int) string {
ans := []byte{}
for columnNumber != 0 {
left := (columnNumber-1)%26 + 1
// 1 -- A(65)
ans = append([]byte{byte(left + 64)}, ans...)
columnNumber = (columnNumber - left) / 26
}
return string(ans)
}
// @lc code=end