1. 程式人生 > >【leetcode 簡單】第三十八題 Excel表列名稱

【leetcode 簡單】第三十八題 Excel表列名稱

itl span title 給定 取余 正整數 簡單 exc pre

給定一個正整數,返回它在 Excel 表中相對應的列名稱。

例如,

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

示例 1:

輸入: 1
輸出: "A"

示例 2:

輸入: 28
輸出: "AB"

示例 3:

輸入: 701
輸出: "ZY"


class Solution(object):
    def convertToTitle(self, n):
        """
        :type n: int
        :rtype: str
        
""" #參考10進制數轉二進制數采用除二取余法 result = "" while n != 0: result = chr((n-1)%26+65) + result n = int((n-1)/26) return result

【leetcode 簡單】第三十八題 Excel表列名稱