题目:
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
解答:
#include<string>
//相当于是读一个26进制数,获得字符长度,依次进位的时候*26就好了,
//跟读一个十进制数的方法很相似。
class Solution {
public:
int titleToNumber(string s) {
int a = 0;
for (int i = 0; i<s.length(); a = a * 26 + s[i] - 'A' + 1, i++);
return a;
}
};