168. Excel Sheet Column Title
Posted thinker-pcw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了168. Excel Sheet Column Title相关的知识,希望对你有一定的参考价值。
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
...
Example 1:
Input: 1
Output: "A"
Example 2:
Input: 28
Output: "AB"
Example 3:
Input: 701
Output: "ZY"
来自 <https://leetcode.com/problems/excel-sheet-column-title/description/>
思路:这相当于一个26进制的转换问题,但特殊之处在于,它是从A开始到Z结束,按照题目规定A为1,Z为26,而不是一般情况下的从0到25。我们仍然可以先进行普通的26进制转换,之后再处理这一问题。
1 class Solution(object): 2 def convertToTitle(self, n): 3 """ 4 :type n: int 5 :rtype: str 6 """ 7 yushu = [] 8 while n > 0: 9 yushu.append(n % 26) 10 n = n // 26 11 for i in range(len(yushu)-1): 12 if yushu[i] <= 0: 13 yushu[i+1] = yushu[i + 1] - 1 14 yushu[i] = yushu[i] + 26 15 yushu=yushu[::-1] 16 if yushu[0] == 0: 17 del (yushu[0]) 18 s = ‘‘ 19 for i in yushu: 20 s += chr(i + 64) 21 return s
以上是关于168. Excel Sheet Column Title的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 168. Excel???????????????Excel Sheet Column Title???
java 168. Excel Sheet Column Title.java
java 168. Excel Sheet Column Title.java