168. Excel Sheet Column Title (Easy)

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 
思路:
1.进制转化,将十进制转化为每位以A-Z表示的26进制数;
2.Python中,使用ord()函数将字母转化为整数,使用chr()函数将整数转化回字母;
3.注意结果输出的字符串逆序问题;
class Solution():
    def convertToTitle(self, n):
        """
        :type n: int
        :rtype: str
        """
        str_1 = ''
        while n:
            
            str_1 += chr(ord('A') + (n - 1) % 26) 
            n = (n - 1) / 26
        return str_1[::-1]

相关问题: http://www.cnblogs.com/yancea/p/7504381.html
posted @ 2017-09-11 19:03  Yancea  阅读(128)  评论(0编辑  收藏  举报