One line recursive Python


  • 0

    The basic idea is ord("A") to ord("Z") equal 65 to 90, and chr(65 + (n-1)) handle the cases when n <= 26. Then just recursively call the itself until n == 0.

    class Solution(object):
        def convertToTitle(self, n):
            """
            :type n: int
            :rtype: str
            """
            return self.convertToTitle((n-1)//26) + chr(65 + (n-1) % 26) if n > 0 else ""
    

Log in to reply
 

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