题目
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
题意
简单题。10进制转换为26进制
Python语言
class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
temp = "ZABCDEFGHIJKLMNOPQRSTUVWXY"
res = ""
while n!=0:
res = res + temp[n%26]
if n%26==0:
n = n - 1
n = n / 26
r = ""
for i in range(len(res)-1, -1, -1):
r = r + res[i]
return r
C++语言
class Solution {
public:
string convertToTitle(int n) {
string temp = "ZABCDEFGHIJKLMNOPQRSTUVWXY";
string res = "";
while(n!=0)
{
res = res + temp[n%26];
if(n%26==0)
n = n - 1;
n = n / 26;
}
string r = "";
for(int i=res.length()-1; i>=0; i--)
r = r + res[i];
return r;
}
};