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
利用十进制转化二进制的思想,设计十进制转二十六进制。
在最后的倒叙输出中用到两种方法:
一种为:利用rbegin()及rend();
第二种:简单的循环实现倒叙;
class Solution {
public:
char Change(int val, int &number){
char x;
if(val == 0){
number--;
x = 'Z';
}
else{
x = val + 'A' - 1;
}
return x;
}
string convertToTitle(int n) {
int number = n;
string s1;
int val;
while(number > 26){
val = number%26;
number = number/26;
s1 += Change(val, number);
}
s1 += number + 'A' - 1;
return string(s1.rbegin(), s1.rend());
}
};
第二种逆序输出:
int len = s1.size();
string s;
for(int i = len-1; i >= 0; i--)
s += s1[i];
return s;