Problem
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 |
|
Example 1:
1 |
|
Example 2:
1 |
|
Example 3:
1 |
|
Explanation
-
We know 1 to 26 is
A-Z
, 27 isAA
, so we need to use module 26 to get the remainder. Ifn
is 28, answer should beAB
. We know 28 % 26 = 2, then we get'A'+2-1 = 'B'
. We get one of the result character, then we divide 26, we get 28 / 26 = 1, which is'A'+1-1='A'
. -
At the end, we reverse the string from
'BA'
to'AB'
. Note: Special case is ifn
is 26, 26 % 26 = 0,'A'+0-1
doesn’t give us any letter. To solve this, we can--n
first, then get the result character by--n%26+'A'
, which isZ
.
Solution
1 |
|