卖萌的弱渣

I am stupid, I am hungry.

Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

1
2
3
4
5
6
7
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB 

Solution

(Excel-Sheet-Column-Title.py) download
1
2
3
4
5
6
7
8
9
10
11
12
class Solution(object):
    def convertToTitle(self, n):
        """
        :type n: int
        :rtype: str
        """
        # ord('A') = 65
        ret = ""
        while n > 0:
            ret = unichr(65+(n-1)%26)+ret
            n = (n-1)/26
        return ret