cpp
class Solution {
public:
string convertToTitle(int columnNumber) {
int sum = columnNumber;
long cols = 0;
long tmp = 1;
while(columnNumber){
tmp = columnNumber;
columnNumber/=26;
if(tmp%26==0){
--columnNumber;
}
++cols;
}
std::cout<<"cols: "<<cols<<std::endl;
std::string res="";
int a = 0;
for(int i=1;i<=cols;++i){
sum-=pow(26,i-1);
}
//类比十进制 -> 特殊的26进制
for(int i = cols;i>1;--i){
a = pow(26,i-1);
res+=(sum/a+'A');
sum=sum%a;
}
res+=('A'+sum);
return res;
}
};
cpp
class Solution {
public:
int titleToNumber(string columnTitle) {
int res = 0;
int a = 0;
for(int i = columnTitle.size()-1;i>=1;--i){
a=pow(26,i);
res+=a;
}
for(int i = 0;i< columnTitle.size();++i){
a = pow(26,columnTitle.size()-1-i);
res += a*(columnTitle[i]-'A');
}
return res+1;
}
};