Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28
思路:26进制转换
代码1:
class Solution {
public:
int titleToNumber(string s) {
int res=0;
for(char ch:s){
char temp = ch-'A'+1;
res = res*26 + temp;
}
return res;
}
};
代码2:
class Solution {
public:
int titleToNumber(string s) {
int res=0;
for(char ch:s){
char temp = ch-'A'+1;
res = res*26 + temp;
}
return res;
}
};
代码3:
class Solution {
public:
int titleToNumber(string s) {
int res = 0;
for (char c:s) {
res *= 26;
res += c - 'A' + 1;
}
return res;
}
};