leetcode题目171
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从右往左遍历这个字符串,将最低位的值先直接算出来加到sum上,然后使用幂函数根据count计数为第几位,来根据当前的位数计算相应的数值进行叠加,具体书面上我也说不太清楚,,,,,直接上代码了。。。
class Solution {
public:
int titleToNumber(string s) {
string::iterator it = s.end()-1;
int sum = 0;
int temp;
int count = 0;
while (it >= s.begin())
{
if (it == s.end()-1)
sum += *it - 'A' + 1;
else
sum += (*it-'A'+1) * pow(26, count);
it--;
count++;
}
return sum;
}
};