File tree Expand file tree Collapse file tree 1 file changed +71
-0
lines changed Expand file tree Collapse file tree 1 file changed +71
-0
lines changed Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments