Python code for excel sheet column title


  • 0
    A
    def convertToTitle(n):
        """
        :type n: int
        :rtype: str
        """
            
        n = n-1 
            
        li = [n%26]
            
        while n/26>0:
            n/=26
            n-=1
            li.append(n%26)
                
        l = len(li)
                   
        str = ""
            
        for idx in xrange(l):
            str += chr(ord('A')+li[l-1-idx]) 
            #ord(char) returns an integer representing the Unicode code point of the character
                
        return str 
    

Log in to reply
 

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