您的位置:首页 > 其它

Leetcode#168. Excel Sheet Column Title(Excel表列名--进制转换)

2018-01-27 17:19 369 查看

题目

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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: