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
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
int titleToNumber(string s) {
int sum = 0;
for(int i = 0; s[i] != '\0'; i++)
{
sum = sum *26 + (s[i] - 'A' + 1);//看作26进制
}
cout << endl;
return sum;
}
};
本文介绍了一种将Excel工作表中的列标题转换为其对应数字编号的算法实现。该算法采用26进制数系统来解析由大写字母组成的列标题,并将其转换为相应的整数形式,例如A对应1,Z对应26,而AA则对应27等。
452

被折叠的 条评论
为什么被折叠?



