题目链接:https://oj.leetcode.com/problems/excel-sheet-column-number/
Runtimes:16ms
1、问题
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
2、分析
简单说就是一个进制转换的问题——从26进制转换成为10进制。
这里需要记住几个重要的ASCII码:’0’ - 48, ‘A’ - 65,’a’ - 97
3、小结
举个栗子:
(CBA)26=(′C′−64)∗262+(′B′−64)∗261+(′A′−64)∗260
4、实现
class Solution {
public:
int titleToNumber(string s) {
int sum = , c = ;
for(int i = s.length() - ; i >= ; i--)
{
sum += (s[i] - ) * c;
c *= ;
}
return sum;
}
};
5、反思
简单的进制题目,可以举一反三类推到其他进制转换。