leetcode -- Excel Sheet Column Title -- 简单要看

https://leetcode.com/problems/excel-sheet-column-title/

similar question
https://leetcode.com/problems/excel-sheet-column-number/

使用chr()以及ord()

class Solution(object):
    def convertToTitle(self, n):
        """ :type n: int :rtype: str """
        if n == 0:
            return ""

        mydict = {}
        tmp = ord('A')
        for i in xrange(26):
            mydict[i] = chr(tmp)
            tmp += 1

        res = ""
        while n:
            mod = (n - 1) % 26 #在这里n减去1,而不是在一开始就减去1
            res += str(mydict[mod])
            n = (n - 1)/26 #在这里n减去1,而不是在一开始就减去1
        return res[::-1]

你可能感兴趣的:(LeetCode)