10进制转换成26进制 A对应0:(正常的情况)
class Solution: def convertToTitle(self, columnNumber: int) -> str: mydict = {} # 创建字典,映射数字到字母 for i in range(1, 27): mydict[i] = chr(ord('A') + i - 1) print(mydict) res_list = [] # 将数字转换为字母 while columnNumber > 0: res_list.append(columnNumber % 26) columnNumber = columnNumber // 26 print(res_list) ex_list = [0] * len(res_list) for i in range(len(res_list)): ex_list[i] = mydict[res_list[i]] ex_list.reverse() res_str = ''.join(ex_list) return res_str # 测试 s = Solution().convertToTitle(columnNumber=28) print(s)
10进制转换成26进制 A对应1:
class Solution: def convertToTitle(self, columnNumber: int) -> str: mydict = {} # 创建字典,映射数字到字母 for i in range(1, 27): mydict[i] = chr(ord('A') + i - 1) print(mydict) res_list = [] # 将数字转换为字母 while columnNumber > 0: res_list.append((columnNumber - 1) % 26 + 1) columnNumber = (columnNumber - 1) // 26 print(res_list) ex_list = [0] * len(res_list) for i in range(len(res_list)): ex_list[i] = mydict[res_list[i]] ex_list.reverse() res_str = ''.join(ex_list) return res_str # 测试 s = Solution().convertToTitle(columnNumber=28) print(s)
标签:10,26,进制,columnNumber,res,list,ex,mydict From: https://www.cnblogs.com/yyyjw/p/18050415