leetcode 171

时间:2021-06-13 05:37:49

171. Excel Sheet Column Number

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 从A到Z依次代表从1到26,按照26进制进位。
代码如下:
 class Solution {
public:
int titleToNumber(string s) {
int n = s.length();
int m = n;
int result = ;
if(n == )
{
return ;
}
result = (int)s[m-];
result -= ;
for(int i = ; i < n+; i++)
{
m--;
if(m == )
{
return result;
}
int p = ;
for(int j = ; j < i; j++)
{
p *= ;
}
int re = (int)s[m-];
re -= ;
result = result + re * p;
}
return result;
}
};