python 练习题- letcode 168. Excel表列名称

题目:

 1 正整数和excel之间的转换
 2 
 3 例:
 4 输入:1
 5 输出:a
 6 
 7 输入:26
 8 输出:z
 9 
10 输入:27
11 输出:aa
12 
13 输入:29
14 输出:ac
15 
16 输入:679
17 输出:zc
18 
19 输入:702
20 输出:aaa

 

思路:

1 输入值num
2 
3 num取整、取余
4 
5 取整等于0,则直接根据余数取对应的值并返回
6 
7 如果该整数大于0,则继续循环取值

 

 

代码:

 1 # @Author  :whyCai
 2 # @Time    :2021/2/19 22:11
 3 
 4 
 5 class Solution:
 6     def showExcelStr(self, num: int) -> str:
 7         '''
 8         输入数字,返回对应的字符
 9         :param num: 
10         :return: 
11         '''
12         listExcelStr = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
13         endExcelStr = ''
14 
15         while True:
16             #取整
17             numInt = num // 26
18             # 取余
19             numFloor = num % 26
20             #余数为0时,如26、52时,对应取整需要减1
21             if numFloor == 0:
22                 numInt = numInt -1
23             #根据余数取对应的值
24             endExcelStr = listExcelStr[numFloor - 1] + endExcelStr
25             #满足小于26,跳出循环
26             if numInt == 0 :
27                 break
28             #重新赋值循环
29             num = numInt
30         return endExcelStr
31 
32 print(Solution().showExcelStr(705))

 

 

结果:(emmmmm,有点低 o(╥﹏╥)o)

 

 

 

 

 

 

 

力扣:

https://leetcode-cn.com/problems/excel-sheet-column-title/

posted @ 2021-02-20 00:02  菜小鱼~  阅读(62)  评论(0编辑  收藏  举报