Excel Sheet Column Number

26进制转换,用map reduce一行解决

class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        return reduce(lambda x, y : x * 26 + y, map(lambda x: ord(x) - ord('@'), s))

Excel Sheet Column Title

这道题比它的反转难一些,原因在于这里虽然是26进制,但是需要注意的是没有0的概念,也就是1减1的话就直接跳到之前一位的26去了

class Solution(object):
    def convertToTitle(self, n):
        """
        :type n: int
        :rtype: str
        """
        result = ''
        while n > 0:
            base, rem = divmod(n ,26)
            if rem == 0:
                base -= 1
                rem = 26
            result = chr(64 + rem) + result
            n = base
        return result

results matching ""

    No results matching ""