Problem Description:
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
题目大意:
给定一个正整数,返回对应的Excel表头
Solutions:
实质上就是一个十进制对26进制的转换问题,注意26整数幂的特殊情况对应的行即可。
Code in C++:
class Solution {
public:
string convertToTitle(int n) {
string res="";
while(n>0)
{
int rest = n%26;
n/=26;
if(rest==0){
n--;
res='Z'+res;
}else res= (char)('A'-1+rest)+res;
}
return res;
}
};
近期评论