Simplest python in one loop with simple succint explanation

  • 0

    Lowest numbers range from 1 to 26 before string codes cycle,
    But indices for code array ranges from 0 to 25,
    Each time we get a number n, shift it one place below (convert it to its index in code array), if n is zero (before it could be shifted below find index in code array), do nothing - stop processing

    class Solution(object):
        def convertToTitle(self, n):
            :type n: int
            :rtype: str
            res   = ""
            while n:
                n    = n - 1
                res  += codes[ n%26 ]
                n    //= 26
            return res[::-1]

Log in to reply

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