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 -> 27AB -> 28
思路:观察到,上述对应关系是,将十进制表示成26进制的A-Z,又string可以看成数组。
如果输入是"BCD" = 2 * 26 * 26 + 3 * 26 + 4 = 26 * (2 * 26 + 3) + 4
class Solution { public: int titleToNumber(string s) { int num = 0; for(int i = 0; i < s.size(); ++i) { num = 26 * num + s[i] - 'A' + 1; } return num; } };