-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path168.excel-sheet-column-title.go
80 lines (75 loc) · 1.12 KB
/
168.excel-sheet-column-title.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
/*
* @lc app=leetcode id=168 lang=golang
*
* [168] Excel Sheet Column Title
*
* https://leetcode.com/problems/excel-sheet-column-title/description/
*
* algorithms
* Easy (30.75%)
* Likes: 1276
* Dislikes: 255
* Total Accepted: 223.3K
* Total Submissions: 718.4K
* Testcase Example: '1'
*
* 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"
*
*/
// @lc code=start
func convertToTitle(n int) string {
return convertToTitle1(n)
}
func convertToTitle1(n int) string {
retVal := ""
for n > 0 {
retVal = string('A'+(n-1)%26) + retVal
n = (n - 1) / 26
}
return retVal
}
func convertToTitle1(n int) string {
retVal := ""
for n > 0 {
n = n - 1 // key point
x := 65 + n%26
retVal = string(x) + retVal
n = n / 26
}
return retVal
}
// @lc code=end