Skip to content

Commit 3c39fbb

Browse files
committed
Excel Sheet Column Title
1 parent 48a85b5 commit 3c39fbb

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

Excel_Sheet_Column_Title.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Given a positive integer, return its corresponding column title as appear in an Excel sheet.
2+
#
3+
# For example:
4+
#
5+
# 1 -> A
6+
# 2 -> B
7+
# 3 -> C
8+
# ...
9+
# 26 -> Z
10+
# 27 -> AA
11+
# 28 -> AB
12+
# ...
13+
# Example 1:
14+
#
15+
# Input: 1
16+
# Output: "A"
17+
# Example 2:
18+
#
19+
# Input: 28
20+
# Output: "AB"
21+
# Example 3:
22+
#
23+
# Input: 701
24+
# Output: "ZY"
25+
26+
27+
class Solution:
28+
def convertToTitle(self, n):
29+
dict = {0: "Z",
30+
1: "A",
31+
2: "B",
32+
3: "C",
33+
4: "D",
34+
5: "E",
35+
6: "F",
36+
7: "G",
37+
8: "H",
38+
9: "I",
39+
10: "J",
40+
11: "K",
41+
12: "L",
42+
13: "M",
43+
14: "N",
44+
15: "O",
45+
16: "P",
46+
17: "Q",
47+
18: "R",
48+
19: "S",
49+
20: "T",
50+
21: "U",
51+
22: "V",
52+
23: "W",
53+
24: "X",
54+
25: "Y"
55+
}
56+
arr = []
57+
58+
if n <= 26:
59+
return dict[n % 26]
60+
61+
while n > 0:
62+
r = n % 26
63+
64+
n = n // 26
65+
66+
if (r == 0):
67+
n = n - 1
68+
69+
arr = [dict[r]] + arr
70+
71+
return "".join(arr)

0 commit comments

Comments
 (0)