[leetcode: Python] 168. Excel Sheet Column Title

来源:互联网 发布:日本经营管理签证知乎 编辑:程序博客网 时间:2024/05/18 14:45

题目:
Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

    1 -> A    2 -> B    3 -> C    ...    26 -> Z    27 -> AA    28 -> AB 

方法一:性能59ms

class Solution(object):    def convertToTitle(self, n):        """        :type n: int        :rtype: str        """        res = ''        while n:            k = (n - 1) % 26            res = chr(k + 65) + res            n = (n - 1) / 26        return res

方法二:性能29ms

class Solution(object):    """    :type n: int    :rtype: str    """    def convertToTitle(self, n):        res = ""        while n >= 1:            n -= 1            res = chr(n%26 + ord("A")) + res            n = n/26        return res
0 0
原创粉丝点击