A relative concise Python solution


  • 0
    P
    class Solution:
        # @return a string
        def convertToTitle(self, num):
            s = ''
            while num:
                temp = num%26
                if not temp:
                    s += 'Z'
                    num = num/26-1
                else:
                    s += chr(temp+64)
                    num /= 26
            return s[::-1]

  • 1
    C
    def convertToTitle(self, n):
    
        d = {i+1: chr(ord('A') + i) for i in range(26)}
    
        r= ''
    
        while n > 0:
            l = n % 26 or 26
            r = d[l] + r
            n = (n-l) / 26
        return r

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.