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
这道题其实就是十进制转换成26进制,那么除以26然后得到余数就可以。注意的是26的时候的处理,然后就是需要字符串反转输出。
class Solution { public: string convertToTitle(int n) { //这个题可以转换成10进制转换成26进制 string s; while(n>0) { int temp = n%26; char c; if(temp == 0) c = 'Z'; else c = 'A'+ temp-1; //cout<<c<<endl; s.push_back(c); n = n/26; if(temp == 0) n = n-1; } //cout<<s<<endl; string s1; int j = 0; for(int i = s.length()-1;i>=0;i--) { //cout<<s[i]<<endl; s1.push_back(s[i]); } return s1; } };