Leetcode 算法题 168
题名:Excel Sheet Column Title
https://leetcode.com/problems/excel-sheet-column-title/#/description
题目描述:给定一个正整数,求出其在Excel中对应的字符编号
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
解题思路:由于Excel中字符编号是26进制的,因此就相当于将数字转换为26进制的表示形式,然后使用Ascii碼的表示形式进行转化
Tips:进行进制转化时需要将数字转换成从0开始的形式
Python代码如下
class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
ls = []
while n > 0:
ls.insert(0, chr((n-1) % 26+65))
n = (n-1)/26
return ''.join(ls)