题目
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
...
AAA -> 703
AAB -> 704
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
分析
用过Execl的都知道,上述字母是其列名称,数字代表第几列,本题是求给定一个A~Z组成的字符串序列,求出其在Execl表中的列数。
由以上给出的示例可以看出规律,其中A~Z代表数字1~26,那么字符串怎么求呢?
比如(由低到高逐位计算):
AC代码
class Solution {
public:
int titleToNumber(string s) {
int len = strlen(s.c_str());
if (len == 0)
return 0;
int count = 0 , p = 0;
for (int i = len-1; i >= 0; i--)
{
count = count + Num(s[i]) * pow(26, p);
p++;
}
return count;
}
int Num(char c)
{
switch (c)
{
case 'A':
return 1; break;
case 'B':
return 2; break;
case 'C':
return 3; break;
case 'D':
return 4; break;
case 'E':
return 5; break;
case 'F':
return 6; break;
case 'G':
return 7; break;
case 'H':
return 8; break;
case 'I':
return 9; break;
case 'J':
return 10; break;
case 'K':
return 11; break;
case 'L':
return 12; break;
case 'M':
return 13; break;
case 'N':
return 14; break;
case 'O':
return 15; break;
case 'P':
return 16; break;
case 'Q':
return 17; break;
case 'R':
return 18; break;
case 'S':
return 19; break;
case 'T':
return 20; break;
case 'U':
return 21; break;
case 'V':
return 22; break;
case 'W':
return 23; break;
case 'X':
return 24; break;
case 'Y':
return 25; break;
case 'Z':
return 26; break;
default:
return 0; break;
}
}
};